The courses you entered are: Grades Hours A 4 A Resulting in a GPA of 0.0 un N The courses you entered are: Grades Hours A 3 В 4 A 5 в 2. Resulting in a GPA of 3.6 n
Hello! I am new to Java and am struggling heavily with a current assignment. I got the output to look as desired, but the respective GPA is not functioning at all.
My logic is to convert each letter grade into a number and multiply that by credit hours in that class. I will then add the result of this calculation for each class together and divide it by the total number of credits the student is undertaking.
I have included images of the criteria model output (white) compared to my own (grey).
I should add that we are intended to use a "do while" loop, but I could not get mine to break, which is why I added an additional break statement.
import java.util.Scanner;
public class GpaCalculator {
public static void main (String args[]){
Scanner inKey = new Scanner (System.in);
double gpa = 3;
String letInput = "";
int grade = 0,earned = 0,finHrs = 0;
String outResult = "";
int hourInput = 0;
do {
System.out.println("What letter grade do you have in this Class? (Without +/-) (Enter Q to quit)");
letInput = inKey.nextLine();
if (letInput.toUpperCase().equals("Q")){
break;
}
if (letInput.toUpperCase().equals("A")) {
grade = 4;
}
if (letInput.toUpperCase().equals("B")) {
grade = 3;
}
if (letInput.toUpperCase().equals("C")) {
grade = 2;
}
if (letInput.toUpperCase().equals("D")) {
grade = 1;
} else {
grade = 0;
}
System.out.println("How many hours was this class worth?");
hourInput = Integer.parseInt(inKey.nextLine());
finHrs += hourInput;
earned += grade * hourInput;
outResult += letInput+"\t\t"+ hourInput + "\n";
}while(!letInput.toUpperCase().equals("Q"));
double newEarned = earned,newHrs = finHrs;
gpa = newEarned/newHrs;
String gradeDisplay = "The courses you entered are:\nGrades\tHours\n"+outResult+"\nResulting in a GPA of "+gpa;
System.out.println(gradeDisplay);
}
}
Step by step
Solved in 2 steps with 1 images