java - 简单的java程序出错

标签 java

我正在自学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 中运行调试功能,发现当我尝试设置 operand = input.nextDouble 时,问题出现在 getOperand 方法的第 29 行。但是,我不明白为什么这会成为一个问题。

最佳答案

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

Scanner keyboard = new Scanner(System.in);

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

根据javadoc ,

When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

关于java - 简单的java程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24659149/

相关文章:

java - 为什么eclipse中h2和mysql数据库位置不同?

java - 将 int 添加到 short

java - 在 Java 中使用插入排序实现无法正常工作快速排序?

java - 如何包含 java src 代码,以便我可以在 Android Studio 中使用它

java - FetchMode.JOIN 急切地获取成员列表显然会产生笛卡尔积

java - 如何使用 jsp 获得最佳性能?

java - Rx 将 Observable<List<T>> 转换为 Observable<T>

java - 如何在 Java 或 AS3 中创建和旋转三维矩阵

java - 将类文件直接添加到 WAS 7 中分解部署的 Ear 中的后果

java - 如何在 Mac OS 上用 C++ 实现命名管道?