有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当我第二次按键盘输入时,java My DoWhile循环中断

我只花了几个星期的时间编写代码,所以我是个新手。这是一个帮助用户定制新车的简单程序。程序对每个菜单使用不同的方法。每个非主要的方法都会询问用户他们想要两个定制汽车选项中的哪一个,然后该方法会将成本发回主要的方法。每个非main的方法都使用与方法engineCost相同的格式。我不明白为什么我的键盘输入只在do while循环的第一次迭代中起作用。这是eclipse上提示我的错误:

Exception in thread "main" java.lang.IllegalStateException

我对堆栈溢出做了一些研究,但我认为我没有足够的知识来正确地搜索答案。任何帮助都将不胜感激

import java.util.Scanner;

public class Ch5HW {
    public static void main(String[] args){
        double baseCar = 14000;
        double addCost = 0;
        double ttlCost = 0;
        int userOpt;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Basic Car includes: Silver color, 4 cilinder engine, Cooper Tires; cost: $14,000");
        System.out.println("");
        //this do-while loops until the user enters 4 to exit 
        do{
            System.out.println("Build Car Quote");
            System.out.println("1. Engine");
            System.out.println("2. Color");
            System.out.println("3. Tires");
            System.out.println("4. Exit");

            System.out.print("Enter Option: ");
            userOpt = 0;
            // this keyboard input only works on the first iteration of the loop
            userOpt = keyboard.nextInt();
            System.out.println(userOpt);
            //nested switch to determine userOpt
            switch (userOpt)
            {
            case 1:
                // calls the engineCost method
                addCost = engineCost();
                //adds the value received to a total value
                ttlCost += addCost;
            break;

            case 2:
                //calls the colorOpt method
                addCost = colorOpt();
                //adds the value received to a total value
                ttlCost += addCost;
            break;

            case 3:
                //calls the tireOpt method
                addCost = tireOpt();
                //adds the value received to a total value
                ttlCost += addCost;
            break;
            }
            //System.out.print(addCost + baseCar);
            keyboard.close();
        } 
        while (userOpt !=4);
        //once loop has finished the total of additional purchases is added to the base car price 
        System.out.print("Total cost: " + (ttlCost + baseCar));
    }
    // this method has options for the engine
    static double engineCost()
    {
        double engineCost = 0;
        double addCost =0;
        int engineOpt;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Engine Options: ");
        System.out.println("1. 8 cylinder: $5000.00");
        System.out.println("2. 6 cylinder: $3500.00");
        System.out.println("3. Exit");
        System.out.println("Enter Option: ");
        engineOpt = keyboard.nextInt();

        switch (engineOpt)
        {
        case 1:
            engineCost = 5000.00;
            addCost += engineCost;
        break;
        case 2:
            engineCost = 3500.00;
            addCost += engineCost;
        break;  
        case 3:
            engineCost = 0;
            addCost += engineCost;
        }
        keyboard.close();
        return addCost; 
    }
}

共 (1) 个答案

  1. # 1 楼答案

    这是因为循环中有一行:keyboard.close();。这将在第一次迭代后关闭Scanner。在此之后,您尝试调用nextInt(),它(从docs)将抛出:

    IllegalStateException - if this scanner is closed

    只有在资源使用完毕后,才应关闭该资源

    也就是说,不要关闭System.in。一般的规则是,如果你没有打开它,你就不应该关闭它