2016-09-20 28 views
-4

所以我为我的类创建了一个gpa计算器。 一切似乎看起来很好,似乎它会正常工作,但由于一些奇怪的原因,当我尝试运行它时,我得到这些错误。 什么导致他们? 它真让人沮丧。Java在stringinput上抛出异常

Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Scanner.java:864) 
    at java.util.Scanner.next(Scanner.java:1485) 
    at java.util.Scanner.nextDouble(Scanner.java:2413) 
    at GpaCalculator.main(GpaCalculator.java:22) 

的源代码:

import java.util.*; 

public class GpaCalculator { 
    public static void main(String[] args) { 

     Scanner numberinput = new Scanner(System.in); 
     Scanner stringinput = new Scanner(System.in); 
     // this program will calculate the gpa of four classes 
     double A,B,C,D,F; 
     double grade,grade2,grade3,grade4; 
     double gpa = 0,name,Course2,Course3,Course4; 
     int Course1; 

     A = 4.0; 
     B = 3.0; 
     C = 2.0; 
     D = 1.0; 
     F = 0.0; 

     System.out.println("Please enter your name>>>"); 
     name = stringinput.nextDouble(); 

     System.out.println("Please enter your course>>>"); 
     Course1 = stringinput.nextInt(); 

     System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>"); 
     String weight = stringinput.nextLine(); 

     System.out.println("What is your grade in the class?>>>"); 
     grade = numberinput.nextDouble(); 

     if (weight.equalsIgnoreCase("honors")) 
      gpa = gpa + 1; 
     else if (weight.equalsIgnoreCase("Ap")) 
      gpa = gpa + 1.5; 
     else if (weight.equalsIgnoreCase("normal")); 

     if (grade >= 100) 
      grade = A; 
     else if (grade >= 91) 
      grade = B; 
     else if (grade >= 83) 
      grade = C; 
     else if (grade >= 75) 
      grade = D; 
     else if (grade >= 67) 
      grade = F; 

     System.out.println("Please enter your course>>>"); 
     Course2 = stringinput.nextDouble(); 

     System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>"); 
     String weight2 = stringinput.nextLine(); 

     System.out.println("What is your grade in the class?>>>"); 
     grade2 = numberinput.nextDouble(); 

     if (weight2.equalsIgnoreCase("honors")) 
      gpa = gpa + 1; 
     else if (weight2.equalsIgnoreCase("Ap")) 
      gpa = gpa + 1.5; 
     else if (weight2.equalsIgnoreCase("normal")); 

     if (grade2 >= 100) 
      System.out.println(A); 
     else if (grade2 >= 91) 
      System.out.println(B); 
     else if (grade2 >= 83) 
      System.out.println(C); 
     else if (grade2 >= 75) 
      System.out.println(D); 
     else if (grade2 >= 67) 
      System.out.println(F); 

     System.out.println("Please enter your course>>>"); 
     Course3 = stringinput.nextDouble(); 

     System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>"); 
     String weight3 = stringinput.nextLine(); 

     System.out.println("What is your grade in the class?>>>"); 
     grade3 = numberinput.nextDouble(); 

     if (weight3.equalsIgnoreCase("honors")) 
      gpa = gpa + 1; 
     else if (weight3.equalsIgnoreCase("Ap")) 
      gpa = gpa + 1.5; 
     else if (weight3.equalsIgnoreCase("normal")); 

     if (grade3 >= 100) 
      System.out.println(A); 
     else if (grade3 >= 91) 
      System.out.println(B); 
     else if (grade3 >= 83) 
      System.out.println(C); 
     else if (grade3 >= 75) 
      System.out.println(D); 
     else if (grade3 >= 67) 
      System.out.println(F); 

     System.out.println("Please enter your course>>>"); 
     Course4 = stringinput.nextDouble(); 

     System.out.println("What is the weight of this class? Normal, Honors, or AP?>>>"); 
     String weight4 = stringinput.nextLine(); 

     System.out.println("What is your grade in the class?>>>"); 
     grade4 = numberinput.nextDouble(); 

     if (weight4.equalsIgnoreCase("honors")) 
      gpa = gpa + 1; 
     else if (weight4.equalsIgnoreCase("Ap")) 
      gpa = gpa + 1.5; 
     else if (weight4.equalsIgnoreCase("normal")); 

     if (grade4 >= 100) 
      System.out.println(A); 
     else if (grade4 >= 91) 
      System.out.println(B); 
     else if (grade4 >= 83) 
      System.out.println(C); 
     else if (grade4 >= 75) 
      System.out.println(D); 
     else if (grade4 >= 67) 
      System.out.println(F); 

     gpa = grade + grade2 + grade3 + grade4/4; 

     System.out.println(name); 
     System.out.println(Course1 + ": " + grade); 
     System.out.println(Course2 + ":" + grade2); 
     System.out.println(Course3 + ": " + grade3); 
     System.out.println(Course4 + ":" + grade4); 
     System.out.println(gpa); 
    } 
} 
+2

等等,你的名字是双?你为什么使用'stringinput.nextDouble'作为名字?不要创建多个扫描器,只需使用一个来检测输入... – Li357

+3

旧的“我有一个异常,但我不能使用它提供的信息来调试我自己的代码”。我应该猜到了...... – John3136

+0

那么我该如何解决例外问题,它不会在问题标签中显示任何问题 –

回答

0

你真的只需要一个Scanner对象。对于每种类型,您不需要两个Scanner对象。

// This is your input in general. 
Scanner input = new Scanner(System.in); 

// This is a string input. 
System.out.println("Enter a string:"); 
String str = input.nextLine(); 

// This is a double input. 
System.out.println("Enter a double:"); 
Double dbl = input.nextDouble();