我的 Main 类中有这段代码。我的问题是,当 GPA 是用总除以类计算时。它没有给我完整的数字。EX 如果总数是 14 并且类是 4 它是 3.5,我的代码只给我一个 3.0。有谁知道为什么,非常感谢您的帮助!
Scanner input = new Scanner(System.in);
System.out.print("How many classes did you have?: ");
int classes = input.nextInt();
String grades = "";
int total = 0;
int dec;
for (int j = 0; j < classes; j++) {
Scanner inputters = new Scanner(System.in);
System.out.print("What is your Grade?: ");
grades = inputters.nextLine();
if (grades.equals("A")){
dec = 4;
total += dec;
} else if (grades.equals("B")){
dec = 3;
total += dec;
} else if (grades.equals("C")){
dec = 2;
total += dec;
} else if (grades.equals("D")){
dec = 1;
total += dec;
} else if (grades.equals("F")){
dec = 0;
total += dec;
}
}
double GPA = total / classes;
System.out.println(GPA);
DecimalFormat formatter = new DecimalFormat("0.##");
System.out.println( formatter.format(GPA));