Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java switch语句在while(true)循环中,文本显示两次_Java_Loops_While Loop_Switch Statement - Fatal编程技术网

Java switch语句在while(true)循环中,文本显示两次

Java switch语句在while(true)循环中,文本显示两次,java,loops,while-loop,switch-statement,Java,Loops,While Loop,Switch Statement,我对while(true)循环中的switch语句有一个问题,基本上文本——“Write action(buy,fill,take):”在我选择“buy”或“fill”时出现两次,但在我选择“take”时只出现一次,原因可能是什么 boolean active = true; while (active) { System.out.println("Write action (buy, fill, take):"); String

我对while(true)循环中的switch语句有一个问题,基本上文本——“Write action(buy,fill,take):”在我选择“buy”或“fill”时出现两次,但在我选择“take”时只出现一次,原因可能是什么

boolean active = true;

        while (active) {
            System.out.println("Write action (buy, fill, take):");
            String action = scanner.nextLine();

            switch(action) {
                case "buy":
                    System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
                    int choiceOfCoffee = scanner.nextInt();
                    if (choiceOfCoffee == 1) {
                        waterInMachine -= waterNeededEspresso;
                        coffeeInMachine -= coffeeNeededEspresso;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfEspresso;
                        break;
                    } else if (choiceOfCoffee == 2) {
                        waterInMachine -= waterNeededLatte;
                        milkInMachine -= milkNeededLatte;
                        coffeeInMachine -= coffeeNeededLatte;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfLatte;
                        break;
                    } else if (choiceOfCoffee == 3) {
                        waterInMachine -= waterNeededCappucino;
                        milkInMachine -= milkNeededCappucino;
                        coffeeInMachine -= coffeeNeededCappucino;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfCappucino;
                        break;
                    }
                    break;
                case "fill":
                    System.out.println("Write how many ml of water do you want to add:");
                    int answer1 = scanner.nextInt();
                    waterInMachine += answer1;
                    System.out.println("Write how many ml of milk do you want to add:");
                    int answer2 = scanner.nextInt();
                    milkInMachine += answer2;
                    System.out.println("Write how many grams of coffee beans do you want to add:");
                    int answer3 = scanner.nextInt();
                    coffeeInMachine += answer3;
                    System.out.println("Write how many disposable cups of coffee do you want to add:");
                    int answer4 = scanner.nextInt();
                    diposableCupsInMachine += answer4;
                    break;
                case "take":
                    System.out.println("I gave you $" + moneyInMachine);
                    moneyInMachine = 0;
                    break;
                case "exit":
                    active = false;
                    break;
            }
        }
这应该行得通

boolean active = true;

        while (active) {
            System.out.println("Write action (buy, fill, take):");
            String action = scanner.nextLine();

            switch(action) {
                case "buy":
                    System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
                    int choiceOfCoffee = Integer.valueOf(scanner.nextLine());
                    if (choiceOfCoffee == 1) {
                        waterInMachine -= waterNeededEspresso;
                        coffeeInMachine -= coffeeNeededEspresso;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfEspresso;
                        break;
                    } else if (choiceOfCoffee == 2) {
                        waterInMachine -= waterNeededLatte;
                        milkInMachine -= milkNeededLatte;
                        coffeeInMachine -= coffeeNeededLatte;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfLatte;
                        break;
                    } else if (choiceOfCoffee == 3) {
                        waterInMachine -= waterNeededCappucino;
                        milkInMachine -= milkNeededCappucino;
                        coffeeInMachine -= coffeeNeededCappucino;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfCappucino;
                        break;
                    }
                    break;
                case "fill":
                    System.out.println("Write how many ml of water do you want to add:");
                    int answer1 = Integer.valueOf(scanner.nextLine());
                    waterInMachine += answer1;
                    System.out.println("Write how many ml of milk do you want to add:");
                    int answer2 = Integer.valueOf(scanner.nextLine());
                    milkInMachine += answer2;
                    System.out.println("Write how many grams of coffee beans do you want to add:");
                    int answer3 = Integer.valueOf(scanner.nextLine());
                    coffeeInMachine += answer3;
                    System.out.println("Write how many disposable cups of coffee do you want to add:");
                    int answer4 = Integer.valueOf(scanner.nextLine());
                    diposableCupsInMachine += answer4;
                    break;
                case "take":
                    System.out.println("I gave you $" + moneyInMachine);
                    moneyInMachine = 0;
                    break;
                case "exit":
                    active = false;
                    break;
            }
        }
这应该行得通

boolean active = true;

        while (active) {
            System.out.println("Write action (buy, fill, take):");
            String action = scanner.nextLine();

            switch(action) {
                case "buy":
                    System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
                    int choiceOfCoffee = Integer.valueOf(scanner.nextLine());
                    if (choiceOfCoffee == 1) {
                        waterInMachine -= waterNeededEspresso;
                        coffeeInMachine -= coffeeNeededEspresso;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfEspresso;
                        break;
                    } else if (choiceOfCoffee == 2) {
                        waterInMachine -= waterNeededLatte;
                        milkInMachine -= milkNeededLatte;
                        coffeeInMachine -= coffeeNeededLatte;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfLatte;
                        break;
                    } else if (choiceOfCoffee == 3) {
                        waterInMachine -= waterNeededCappucino;
                        milkInMachine -= milkNeededCappucino;
                        coffeeInMachine -= coffeeNeededCappucino;
                        diposableCupsInMachine -= 1;
                        moneyInMachine += costOfCappucino;
                        break;
                    }
                    break;
                case "fill":
                    System.out.println("Write how many ml of water do you want to add:");
                    int answer1 = Integer.valueOf(scanner.nextLine());
                    waterInMachine += answer1;
                    System.out.println("Write how many ml of milk do you want to add:");
                    int answer2 = Integer.valueOf(scanner.nextLine());
                    milkInMachine += answer2;
                    System.out.println("Write how many grams of coffee beans do you want to add:");
                    int answer3 = Integer.valueOf(scanner.nextLine());
                    coffeeInMachine += answer3;
                    System.out.println("Write how many disposable cups of coffee do you want to add:");
                    int answer4 = Integer.valueOf(scanner.nextLine());
                    diposableCupsInMachine += answer4;
                    break;
                case "take":
                    System.out.println("I gave you $" + moneyInMachine);
                    moneyInMachine = 0;
                    break;
                case "exit":
                    active = false;
                    break;
            }
        }

谢谢,它起作用了。但是为什么不使用nextInt()?这与扫描仪的工作方式有关。。你可以在这里看到解释-谢谢,它起作用了。但是为什么不使用nextInt()?这与扫描仪的工作方式有关。。你可以在这里看到解释-