扫描仪不适用于.nextDouble

时间:2018-06-29 09:03:12

标签: java

我刚刚学习了一些Java,并尝试制作一个简单的计算器。当我用扫描仪输入用户所需的总数时,它工作正常。那是我使用另一台扫描仪获取破坏代码的数字的时候。它给了我这些错误:

Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Unknown Source)
at java.base/java.util.Scanner.next(Unknown Source)
at java.base/java.util.Scanner.nextDouble(Unknown Source)
at test2.calculator2.input(calculator2.java:45)
at test2.calculator2.main(calculator2.java:128)

代码:

package test2;

import java.util.Scanner;

public class calculator2 {
    public static int cNum, cOp;
    public static double num[], tNums, output;
    public static String op[];

public static void input() {
    boolean isInputting, invalidTNum, invalidOp;

    isInputting = true;
    invalidTNum = true;
    invalidOp = true;
    cNum = 1;
    cOp = 1;

    Scanner tNumInput = new Scanner(System.in);
    System.out.println("Please input a number larger or equal to 2.");
    System.out.println("How many numbers would you like?");
    tNums = tNumInput.nextDouble();

    while(invalidTNum) {
        if(tNums < 2) {
            invalidTNum = true;
            System.out.println("Please input a number larger or equal to 2.");
            System.out.println("How many numbers would you like?");
            tNums = tNumInput.nextDouble();
        } else {
            invalidTNum = false;
            tNumInput.close();
        }
    }

    num = new double[(int)tNums];
    op = new String[(int)tNums --];

    System.out.println("Please input number.");
    Scanner numInput = new Scanner(System.in);
41: num[cNum] = numInput.nextDouble();

    cNum ++;

    while(isInputting) {
        if(cNum >= tNums) {
            isInputting = false;
        } else {
            isInputting = true;
        }

        while(invalidOp) {
            System.out.printf("Please input operation. (+,-,*,/)");
            Scanner opInput = new Scanner(System.in);
            op[cOp] = opInput.nextLine();

            switch(op[cOp]) {
                case"+":
                case"-":
                case"*":
                case"/":
                    invalidOp = false;
                default:
                    invalidOp = true;
                    System.out.println("Your operation is invalid.");
            }
        }

        System.out.printf("Please input number.");
        num[cNum] = numInput.nextDouble();

        cOp ++;
        cNum ++;
        invalidOp = true;
    }


}

public static void count() {
    boolean isCounting = true;
    double awnswer;
    cNum = 1;
    cOp = 1;

    awnswer = num[cNum];
    cNum ++;

    while(isCounting) {
        if(cNum >= tNums) {
            isCounting = false;
        } else {
            isCounting = true;
        }

        switch(op[cOp]) {
            case"+":
                awnswer += num[cNum];
                break;
            case"-":
                awnswer -= num[cNum];
                break;
            case"*":
                awnswer *= num[cNum];
                break;
            case"/":
                awnswer /= num[cNum];
                break;
            default:
                break;
        }

        cNum ++;
        cOp ++;
    }

    output = awnswer;
}

public static void main(String args[]) {
    boolean reuse = true;

    while(reuse) {
128:    input();
        count();
        System.out.println(output);
    }
  }
}

可以这么说,我真的只是从Java开始,我真的很烂,所以如果我犯了一些愚蠢的错误,请原谅我。感谢先进! = 3

3 个答案:

答案 0 :(得分:1)

您不应创建Scanner的多个实例。相反,只需在开始时制作一台扫描仪,然后在整个程序中重复使用该扫描仪即可。

固定版本可能如下所示:

package test2;

import java.util.Scanner;

public class calculator2 {
    public static int cNum, cOp;
    public static double num[], tNums, output;
    public static String op[];
    public static Scanner scanner;

    public static void input() {
        boolean isInputting, invalidTNum, invalidOp;

        isInputting = true;
        invalidTNum = true;
        invalidOp = true;
        cNum = 1;
        cOp = 1;

        System.out.println("Please input a number larger or equal to 2.");
        System.out.println("How many numbers would you like?");
        tNums = scanner.nextDouble();

        while(invalidTNum) {
            if(tNums < 2) {
                invalidTNum = true;
                System.out.println("Please input a number larger or equal to 2.");
                System.out.println("How many numbers would you like?");
                tNums = scanner.nextDouble();
            } else {
                invalidTNum = false;
                scanner.close();
            }
        }

        num = new double[(int)tNums];
        op = new String[(int)tNums --];

        System.out.println("Please input number.");
        41: num[cNum] = scanner.nextDouble();

        cNum ++;

        while(isInputting) {
            if(cNum >= tNums) {
                isInputting = false;
            } else {
                isInputting = true;
            }

            while(invalidOp) {
                System.out.printf("Please input operation. (+,-,*,/)");
                op[cOp] = scanner.nextLine();

                switch(op[cOp]) {
                    case"+":
                    case"-":
                    case"*":
                    case"/":
                    invalidOp = false;
                    default:
                    invalidOp = true;
                    System.out.println("Your operation is invalid.");
                }
            }

            System.out.printf("Please input number.");
            num[cNum] = scanner.nextDouble();

            cOp ++;
            cNum ++;
            invalidOp = true;
        }


    }

    public static void count() {
        boolean isCounting = true;
        double awnswer;
        cNum = 1;
        cOp = 1;

        awnswer = num[cNum];
        cNum ++;

        while(isCounting) {
            if(cNum >= tNums) {
                isCounting = false;
            } else {
                isCounting = true;
            }

            switch(op[cOp]) {
                case"+":
                awnswer += num[cNum];
                break;
                case"-":
                awnswer -= num[cNum];
                break;
                case"*":
                awnswer *= num[cNum];
                break;
                case"/":
                awnswer /= num[cNum];
                break;
                default:
                break;
            }

            cNum ++;
            cOp ++;
        }

        output = awnswer;
    }

    public static void main(String args[]) {
        boolean reuse = true;
        scanner = new Scanner(System.in);

        while(reuse) {
            input();
            count();
            System.out.println(output);
        }
    }
}

答案 1 :(得分:0)

在调用tNumInput.nextDouble();时,应检查扫描仪是否具有Element。否则会出现此错误。

 if(tNumInput.hasNext())
 tNumInput.next();

还要确保调用tNumInput.close();

答案 2 :(得分:0)

我首先认为,Scanner的一个实例就足够了。然后,可能应该设置Locale以正确定义小数点,因为如果使用,作为小数点,则在美国语言环境中将不起作用。

try (Scanner scan = new Scanner(System.in)) {
    scan.useLocale(Locale.US);
    // ...
}