2016-09-20 3 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(); 
Смежные вопросы