Hey guys I'm new to java and was wondering if I could have a little assistance. I need to have a user input their number of classes, hours for each class and the grade they received after the information is given i need to calculate their GPA. I have gotten everything done and i have my algorithm for the GPA configuration but I hit a snag. After the user inputs their info I have no idea how to combine all the information to calculate the GPA. Could someone please assist me with this. Thank you. (also I can't use arrays of any kind)

import java.util.Scanner;

public class StudentGPA {
	
	public static void main (String args[]){
		
		Scanner input = new Scanner (System.in);
		System.out.println("Please enter your number of classes");
		int classes;
		classes = input.nextInt();
		
		for(int count = 0; count < classes; count++)
		{
			int hours;
			double grade;
			
			
			Scanner input2 = new Scanner (System.in);
			System.out.println("Please enter the number of hours this course was");
			hours = input2.nextInt();
			Scanner input3 = new Scanner (System.in);
			System.out.println("Please enter your grade for this class");
			grade = input3.nextInt();
			
			double gpa = (hours * grade)/ classes;
			
			System.out.println(gpa);
		}
	
	

	}
	

}

maybe you could put the result of your calculations for each class in an array then when all the calculations for eahc class is done, get the GPA by looping through the array

You could keep your total credit hours & total credit earned outside the "for" loop. Also, the display line should definitely be outside the loop. Then, you can compute the GPA after you done collecting data.

/*
Example...
Student A enter 4 classes.
  Class 1 with 3 credit hours and the student got an A
  Class 2 with 3 credit hours and the student got an B
  Class 3 with 3 credit hours and the student got an B
  Class 4 with 1 credit hours and the student got an A

Think about how you do it manually...
  totalCredit = 0
  totalCreditEarned = 0

  Loop Start:
  Class 1 data entered...
  totalCredit += 3
  totalCreditEarned += (3 * 4)  // 3 credit hours and 4 for the A
  Class 2 data entered...
  totalCredit += 3
  totalCreditEarned += (3 * 3)  // 3 credit hours and 3 for the B
  Class 3 data entered...
  totalCredit += 3
  totalCreditEarned += (3 * 3)  // 3 credit hours and 3 for the B
  Class 4 data entered...
  totalCredit += 1
  totalCreditEarned += (1 * 4)  // 1 credit hour and 4 for the A

  Now the loop is done, to compute your GPA is
  GPA = totalCreditEarned/totalCredit
*/

Edited 5 Years Ago by Taywin: n/a

You could keep your total credit hours & total credit earned outside the "for" loop. Also, the display line should definitely be outside the loop. Then, you can compute the GPA after you done collecting data.

/*
Example...
Student A enter 4 classes.
  Class 1 with 3 credit hours and the student got an A
  Class 2 with 3 credit hours and the student got an B
  Class 3 with 3 credit hours and the student got an B
  Class 4 with 1 credit hours and the student got an A

Think about how you do it manually...
  totalCredit = 0
  totalCreditEarned = 0

  Loop Start:
  Class 1 data entered...
  totalCredit += 3
  totalCreditEarned += (3 * 4)  // 3 credit hours and 4 for the A
  Class 2 data entered...
  totalCredit += 3
  totalCreditEarned += (3 * 3)  // 3 credit hours and 3 for the B
  Class 3 data entered...
  totalCredit += 3
  totalCreditEarned += (3 * 3)  // 3 credit hours and 3 for the B
  Class 4 data entered...
  totalCredit += 1
  totalCreditEarned += (1 * 4)  // 1 credit hour and 4 for the A

  Now the loop is done, to compute your GPA is
  GPA = totalCreditEarned/totalCredit
*/

Thank you so much works perfectly now I really appreciate it!

This article has been dead for over six months. Start a new discussion instead.