繁体   English   中英

简单的Java程序中的错误

[英]Error in simple java program

我正在自学Java,并且在使用类编写代码时遇到了此错误

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at StateCalculator.getOperand(StateCalculator.java:29)
    at StateCalculator.main(StateCalculator.java:77)

下面是我的代码:

import java.util.Scanner;


public class StateCalculator {
    private double currentValue = 0;

    //Initialize to 0
    public StateCalculator() {
    }

    public static int displayMenu() {
        Scanner keyboard = new Scanner(System.in);
        int menuChoice = 0;

        do {
            System.out.print("Menu\n 1. Add\n 2. Subtract\n 3. Multiply\n 4. Divide\n 5.Clear\n 6. Quit\n What would you like to do?: ");
            menuChoice = keyboard.nextInt();
        } while(menuChoice < 1 || menuChoice > 6);

        keyboard.close();
        return menuChoice;
    }

    public static double getOperand(String prompt) {
        Scanner input = new Scanner(System.in);
        double operand = 0;

        System.out.print(prompt);
        operand = input.nextDouble();

        input.close();
        return operand;
    }

    public double getCurrentValue() {
        return currentValue;
    }

    public void add(double operand) {
        currentValue += operand;
    }

    public void subtract(double operand) {
        currentValue -= operand;
    }

    public void multiply(double operand) {
        currentValue *= operand;
    }

    public void divide(double operand) {
        if(operand == 0) {
            currentValue = Double.NaN;
        }
        else {
            currentValue /= operand;
        }
    }

    public void clear() {
        currentValue = 0;
    }


    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        StateCalculator calculator = new StateCalculator();
        int option;
        double operand;

        do{
            System.out.println("The current value is " + calculator.currentValue);
            option = StateCalculator.displayMenu();

            switch(option) {
            case 1:
                operand = getOperand("What is the second number?: ");
                calculator.add(operand);
                break;
            case 2:
                operand = getOperand("What is the second number?: ");
                calculator.subtract(operand);
                break;
            case 3:
                operand = getOperand("What is the second number?: ");
                calculator.multiply(operand);
                break;
            case 4:
                operand = getOperand("What is the second number?: ");
                calculator.divide(operand);
                break;
            case 5:
                calculator.clear();
                break;
            }

        }while(option != 6);

        keyboard.close();
    }

}

我尝试在eclipse中运行调试功能,并发现在尝试设置操作数= input.nextDouble时,该问题发生在getOperand方法的第29行。 但是,我不明白为什么这会是一个问题。

不要调用keyboard.close(); 当您关闭keyboard (定义的)时

Scanner keyboard = new Scanner(System.in);

它会关闭System.in ,然后您的其他方法将无法工作(因为控制台将不会重新打开)。 您可以在System.in上拥有多个扫描仪(只要您不关闭它们),或者通过一个(或者,但是请不要使用全局扫描仪)。

根据javadoc

关闭Scanner ,如果源实现了Closeable接口,它将关闭其输入源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM