1

这个程序出错后我遇到了错误。在这一点上,事情似乎大部分都井井有条,我可以毫无错误地编译,但是当我运行程序并要求我输入一个字母等级时,输入任何字母等级都会给我这个:

    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);
   }
}
4

2 回答 2

0

当您声明 letterGrade 时,您将其声明为 int 并读取 int

int letterGrade = kbReader.nextInt();

但是然后在你的开关中你使用一个字符

`case 'A':

应该是case 1:或任何其他 int。

于 2013-07-14T20:02:24.660 回答
0
char letterGrade =(char) kbReader.next().toCharArray()[0];

我想这会对你有所帮助。

于 2013-07-14T20:16:07.983 回答