簡體   English   中英

Java 對 stringinput 拋出異常

[英]Java throws exception on stringinput

所以我正在為我的班級創建一個 gpa 計算器。 一切看起來都很好,看起來也能正常工作,但由於某些奇怪的原因,當我嘗試運行它時,我收到了這些錯誤。 是什么導致它們? 它真的讓我很沮喪。

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

你真的只需要一個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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM