我在使用此程序出错后遇到错误。在这一点上,事情看起来大部分都是顺序的,我可以编译而没有错误,但是当我运行程序并且我被要求输入一个字母等级时,输入任何字母等级都会给我这个:
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的新手(在编程的第3天),但非常坚定。
import 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);
}
}
答案 0 :(得分:0)
当你声明letterGrade时,你将它声明为int并读取int
int letterGrade = kbReader.nextInt();
但是在你的开关中你使用了一个char
`case 'A':
应为case 1:
或任何其他int。
答案 1 :(得分:0)
char letterGrade =(char) kbReader.next().toCharArray()[0];
我认为这会对你有帮助。