如果循环导致意外输出时,内部内部切换的语句会导致

发布于 2025-02-10 18:44:34 字数 5875 浏览 2 评论 0原文

肯定有更实用的方式编写以下代码的方法。但是我真的想了解为什么它正在打印行 “写下行动(购买,填充,剩下,退出):“在采取行动后两次。 在询问用户输入的输出后,对我来说没有任何意义,那么如何打印两次? 这发生在动作“购买”和“填充”之后发生,但不要使用“ take”&& “其余的”。我想念什么? (只需运行代码,您就会明白我的意思!)

import java.util.Scanner;

    public class Main {
    
        private static void printMachineState(int water, int milk, int beans, int cups, int money) {
            System.out.printf("The coffee machine has:\n%d ml of water\n%d ml of milk\n%d g of coffee beans\n%d disposable cups\n$%d of money\n", water, milk, beans, cups, money);
        }
    
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            int water = 400;
            int milk = 540;
            int beans = 120;
            int cups = 9;
            int money = 550;
            int coffeeEnum;
            boolean exitFlag = false;
    
            while (!exitFlag) {
                System.out.println("Write action (buy, fill, take, remaining, exit):");
                String input = sc.nextLine();
    
                switch (input) {
                    case "buy":
                        System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 -cappuccino");
                        coffeeEnum = sc.nextInt();
    
                        if (coffeeEnum == 1) {
                            if (water < 250) {
                                System.out.println("Sorry, not enough water!");
                                break;
                            }
                            if (beans < 16) {
                                System.out.println("Sorry, not enough beans!");
                                break;
                            }
                            if (cups == 0) {
                                System.out.println("Sorry, not enough cups!");
                                break;
                            }
                            water -= 250;
                            beans -= 16;
                            money += 4;
                            cups--;
                            System.out.println("I have enough resources, making you a coffee!");
                            break;
                        }
    
                        if (coffeeEnum == 2) {
                            if (water < 350) {
                                System.out.println("Sorry, not enough water!");
                                break;
                            }
                            if (milk < 75) {
                                System.out.println("Sorry, not enough milk!");
                                break;
                            }
                            if (beans < 20) {
                                System.out.println("Sorry, not enough beans!");
                                break;
                            }
                            if (cups <= 0) {
                                System.out.println("Sorry, not enough cups!");
                                break;
                            }
                            water -= 350;
                            milk -= 75;
                            beans -= 20;
                            money += 7;
                            cups--;
                            System.out.println("I have enough resources, making you a coffee!");
                            break;
                        }
    
                        if (coffeeEnum == 3) {
                            if (water < 200) {
                                System.out.println("Sorry, not enough water!");
                                break;
                            }
                            if (milk < 100) {
                                System.out.println("Sorry, not enough milk!");
                                break;
                            }
                            if (beans < 12) {
                                System.out.println("Sorry, not enough beans!");
                                break;
                            }
                            if (cups == 0) {
                                System.out.println("Sorry, not enough cups!");
                                break;
                            }
                            water -= 200;
                            milk -= 100;
                            beans -= 12;
                            money += 6;
                            cups--;
                            System.out.println("I have enough resources, making you a coffee!");
                            break;
                        }
                    case "fill":
                        System.out.println("Write how many ml of water you want to add:");
                        water += sc.nextInt();
                        System.out.println("Write how many ml of milk you want to add:");
                        milk += sc.nextInt();
                        System.out.println("Write how many grams of coffee beans you want to add:");
                        beans += sc.nextInt();
                        System.out.println("Write how many disposable cups of coffee you want to add:");
                        cups += sc.nextInt();
                        printMachineState(water, milk, beans, cups, money);
                        break;
                    case "take":
                        System.out.printf("I gave you %d\n", money);
                        money = 0;
                        break;
                    case "remaining":
                        printMachineState(water, milk, beans, cups, money);
                        break;
                    case "exit":
                        exitFlag = true;
                        break;
                    default:
                        break;
                }
            }
    
        }
    }

There are surely ways to write the following code in a more practical way.But I really want to understand why it is printing the line
"Write action (buy, fill, take, remaining, exit):" two times after an action is preformed.
It makes no sense to me after the output a user input is asked, so how can it be that it is printed twice?
This happens after the action "buy" and "fill" but not with "take" && "remaining". What am I missing?
(Just run the code and you will see what I mean!)

import java.util.Scanner;

    public class Main {
    
        private static void printMachineState(int water, int milk, int beans, int cups, int money) {
            System.out.printf("The coffee machine has:\n%d ml of water\n%d ml of milk\n%d g of coffee beans\n%d disposable cups\n$%d of money\n", water, milk, beans, cups, money);
        }
    
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            int water = 400;
            int milk = 540;
            int beans = 120;
            int cups = 9;
            int money = 550;
            int coffeeEnum;
            boolean exitFlag = false;
    
            while (!exitFlag) {
                System.out.println("Write action (buy, fill, take, remaining, exit):");
                String input = sc.nextLine();
    
                switch (input) {
                    case "buy":
                        System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 -cappuccino");
                        coffeeEnum = sc.nextInt();
    
                        if (coffeeEnum == 1) {
                            if (water < 250) {
                                System.out.println("Sorry, not enough water!");
                                break;
                            }
                            if (beans < 16) {
                                System.out.println("Sorry, not enough beans!");
                                break;
                            }
                            if (cups == 0) {
                                System.out.println("Sorry, not enough cups!");
                                break;
                            }
                            water -= 250;
                            beans -= 16;
                            money += 4;
                            cups--;
                            System.out.println("I have enough resources, making you a coffee!");
                            break;
                        }
    
                        if (coffeeEnum == 2) {
                            if (water < 350) {
                                System.out.println("Sorry, not enough water!");
                                break;
                            }
                            if (milk < 75) {
                                System.out.println("Sorry, not enough milk!");
                                break;
                            }
                            if (beans < 20) {
                                System.out.println("Sorry, not enough beans!");
                                break;
                            }
                            if (cups <= 0) {
                                System.out.println("Sorry, not enough cups!");
                                break;
                            }
                            water -= 350;
                            milk -= 75;
                            beans -= 20;
                            money += 7;
                            cups--;
                            System.out.println("I have enough resources, making you a coffee!");
                            break;
                        }
    
                        if (coffeeEnum == 3) {
                            if (water < 200) {
                                System.out.println("Sorry, not enough water!");
                                break;
                            }
                            if (milk < 100) {
                                System.out.println("Sorry, not enough milk!");
                                break;
                            }
                            if (beans < 12) {
                                System.out.println("Sorry, not enough beans!");
                                break;
                            }
                            if (cups == 0) {
                                System.out.println("Sorry, not enough cups!");
                                break;
                            }
                            water -= 200;
                            milk -= 100;
                            beans -= 12;
                            money += 6;
                            cups--;
                            System.out.println("I have enough resources, making you a coffee!");
                            break;
                        }
                    case "fill":
                        System.out.println("Write how many ml of water you want to add:");
                        water += sc.nextInt();
                        System.out.println("Write how many ml of milk you want to add:");
                        milk += sc.nextInt();
                        System.out.println("Write how many grams of coffee beans you want to add:");
                        beans += sc.nextInt();
                        System.out.println("Write how many disposable cups of coffee you want to add:");
                        cups += sc.nextInt();
                        printMachineState(water, milk, beans, cups, money);
                        break;
                    case "take":
                        System.out.printf("I gave you %d\n", money);
                        money = 0;
                        break;
                    case "remaining":
                        printMachineState(water, milk, beans, cups, money);
                        break;
                    case "exit":
                        exitFlag = true;
                        break;
                    default:
                        break;
                }
            }
    
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2025-02-17 18:44:35

扫描仪方法nextInt()仅接收数字,但在此之后被送入扫描仪的新线。因此,nextline()被称为nextline()获取newline并跳到默认开关情况,然后开始循环并再次打印行。

一个简单快速的修复方法是将扫描仪sc的实例化移至WARE循环,以便每次循环时,都会制作一个新的“清洁”空扫描仪。

The scanner method nextInt() only receives the number, but not the newline that is fed into the scanner after that. So the time the nextLine() gets called it gets the newline and jumps to the default switch case, starting the while loops over and printing the line again.

An easy and quick fix was to move the instantiation of scanner sc into the while loop so that every time it loops, a new "clean" empty scanner is made.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文