这个程序出错后我遇到了错误。在这一点上,事情似乎大部分都井井有条,我可以毫无错误地编译,但是当我运行程序并要求我输入一个字母等级时,输入任何字母等级都会给我这个:
Exception in thread "main" java.util.InputMisMatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at GPA.main(GPA.java:28)
非常感谢任何帮助。请原谅我的菜鸟,我是 Java 的新手(在我编程的第三天),但非常坚定。
导入 java.util.Scanner;公共课GPA
{
public static void main(String[] args)
{
Scanner kbReader = new Scanner(System.in);
System.out.println("Enter number of classes: ");
int numberClasses = kbReader.nextInt();
int i;
int totalCreditForGPA = 0;
int totalCreditHours = 0;
for (i = 0; i < numberClasses; i++)
{
System.out.print("Enter letter grade of class: ");
int letterGrade = kbReader.nextInt();
System.out.print("Enter credit hour of class: ");
int creditHour = kbReader.nextInt();
switch (letterGrade)
{
case 'A':
case 'a': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 4);
totalCreditHours = totalCreditHours + creditHour;
break;
case 'B':
case 'b': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 3);
totalCreditHours = totalCreditHours + creditHour;
break;
case 'C':
case 'c': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 2);
totalCreditHours = totalCreditHours + creditHour;
break;
case 'D':
case 'd': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 1);
totalCreditHours = totalCreditHours + creditHour;
break;
case 'F':
case 'f': totalCreditForGPA = (int) totalCreditForGPA + (creditHour * 0);
totalCreditHours = totalCreditHours + creditHour;
break;
default:
System.out.println("Invalid letter grade.");
}
}
int GPA = totalCreditForGPA;
System.out.println("Your GPA is " + GPA);
}
}