переключить оператор в то время как (истина) l oop, текст появляется дважды - PullRequest
0 голосов
/ 06 февраля 2020

У меня есть проблема с оператором switch через некоторое время (true) l oop, в основном текст - «Писать действие (купить, заполнить, взять):» появляется дважды, когда я выбираю «купить» или «заполнить», но только один раз, как это должно быть, когда я выбираю «взять», в чем может быть причина?

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;
            }
        }

1 Ответ

2 голосов
/ 06 февраля 2020

Это должно работать ..

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;
            }
        }
...