java - 在用户回答"is"后尝试让程序返回主菜单以继续,或者如果用户回答“否”则让程序结束并显示总数消息

标签 java methods while-loop reset continue

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        boolean start = true;
        while(start)

                System.out.printf("%70s %n", " @@@@@     Zoos Australia     @@@@@ " + "\n");

                System.out.printf("%57s %n", "Main Menu" + "\n");

                System.out.printf("%72s %n", "Zoo has the following ticketing options:");

                System.out.print("\n");

                System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
                System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
                System.out.printf("%60s %n", "3 = Senior (60+ yrs)");

                System.out.println("\n");

                String choose1 = "";
                String choose2 = "";
                String choose3 = "";
                String selected = "";
                int option = 0;
                {
                    System.out.print("Please select an option: ");
                    option = input.nextInt();
                    if (option == 1) {
                        choose1 = "Child";
                        selected = choose1;
                    } else if (option == 2) {
                        choose2 = "Adult";
                        selected = choose2;
                    } else if (option == 3) {
                        choose3 = "Senior";
                        selected = choose3;
                    }
                }
                // done

                System.out.println("\n");


                int price = 0;
                int tickets = 0;
                System.out.print("Enter the number of tickets: ");
                tickets = input.nextInt();
                if (selected == choose1) {
                    price = 10;
                } else if (selected == choose2) {
                    price = 20;
                } else if (selected == choose3) {
                    price = 15;
                }


                System.out.println("\n");

                System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");

                System.out.println("\n");

                int confirm = 0;
                    System.out.print("Press 1 to confirm purchase: ");
                    confirm = input.nextInt();
                    if (confirm != 1) {
                        System.out.print("Incorrect Key. Please return to Main Menu");
                        System.out.println("\n");

                    } else {
                        break;
                    }


                System.out.println("\n");

                int total = tickets;
                price = total * price;
                System.out.print("Total amount for " + selected + " tickets: " + "$" + price);

                System.out.println("\n");

                String pick = "";
                System.out.print("Do you wish to continue: ");
                input.next();

                System.out.println("\n");
                if (pick == "no") {
                    System.out.print("Total amount payable is: " + "$" + price);
                    System.out.println("\n");
                    System.out.print("Have a nice day!");
                    System.out.println("\n");
                }}}

尝试在程序结束时执行此操作,其中使用某种方法或其他方法询问用户“您想继续吗”,但无法使其正常工作。程序要么仅返回主菜单,要么程序结束并显示总消息“应付总额...”等。我尝试使用 while 与 continue 和 break 。使用 boolean 值与 true 和 false。但没有运气。谢谢任何能为我解决这个问题的人。

最佳答案

首先,您必须将用户的输入分配给一个变量:pick = input.next()。之后,问题是您使用 == 运算符将用户的输入字符串与“no”字符串进行比较。当比较引用类型(对象)(并且 String 是一个对象)时,大多数情况下 == 运算符会给您带来不可预测的结果,因为它比较引用(内存中对象的地址)而不是实际内容。请记住,您始终必须使用 .equals() 方法。当用户输入“否”时,您还必须从循环中中断。

有很多关于这个问题的 Material 。例如,您可以检查这个 How do I compare strings in Java?

附注我快速查看了您的其余代码并添加了一些附加注释,这可能会帮助您改进它。祝学习 Java 顺利!

    Scanner input = new Scanner(System.in);
    // boolean start = true; you don't need this line
    while(true) { // 'true' condition makes it an infinite loop until you use break
                  // You also have to surround your while loop with curly braces, 
                  // otherwise you fall into an infinite loop
        System.out.printf("%70s %n", " @@@@@     Zoos Australia     @@@@@ \n");
        System.out.printf("%57s %n", "Main Menu\n");
        System.out.printf("%72s %n", "Zoo has the following ticketing options: \n");
        System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
        System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
        System.out.printf("%60s %n", "3 = Senior (60+ yrs)\n");

        String choose1 = "";
        String choose2 = "";
        String choose3 = "";
        String selected = "";
        int option = 0;
        System.out.print("Please select an option: ");
        option = input.nextInt();
        if (option == 1) {
            choose1 = "Child";
            selected = choose1;
        } else if (option == 2) {
            choose2 = "Adult";
            selected = choose2;
        } else if (option == 3) {
            choose3 = "Senior";
            selected = choose3;
        }
        System.out.println(); // "\n" is a redundant argument

        int price = 0;
        int tickets = 0;
        System.out.print("Enter the number of tickets: ");
        tickets = input.nextInt();
        if (selected.equals(choose1)) { // you should never compare strings with == operator! Always use .equals() instead
            price = 10;
        } else if (selected.equals(choose2)) {
            price = 20;
        } else if (selected.equals(choose3)) {
            price = 15;
        }
        System.out.println();
        System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");
        System.out.println();

        int confirm = 0;
        System.out.print("Press 1 to confirm purchase: ");
        confirm = input.nextInt();
        if (confirm != 1) {
            System.out.print("Incorrect Key. Please return to Main Menu");
            System.out.println("\n");
        } else {
            //break; you cannot use 'break' in the if statement! You have to figure out another way, how to handle an invalid input
        }
        System.out.println();

        int total = tickets;
        price = total * price;
        System.out.print("Total amount for " + selected + " tickets: " + "$" + price);
        System.out.println();

        String pick = "";
        System.out.print("Do you wish to continue: ");
        pick = input.next(); // you have to assign the input to a variable
        System.out.println();

        if (pick.equals("no")) { // You have to ALWAYS use .equals() when comparing Strings or any other reference types! == works correctly only with primitive types
            System.out.print("Total amount payable is: " + "$" + price);
            System.out.println();
            System.out.print("Have a nice day!");
            System.out.println();
            break; // you need to break from the loop in the end
        }
    }
}

关于java - 在用户回答"is"后尝试让程序返回主菜单以继续,或者如果用户回答“否”则让程序结束并显示总数消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61399011/

相关文章:

java - Tomcat 无法从 Eclipse 启动并出现 JVM Launcher 异常

java - 通过创建一种方法来简化 Java 代码……但是在哪里?

c# - 使用泛型方法时如何正确约束相关类类型?

c++ - 在开关 block 中

bash - 读取文件中的每一行,替换变量并将输出发送到文件 bash

java - 在 REST api 中一次发布复杂对象还是逐步添加子资源?

java - 使用 Restito 模拟的反馈

java - 覆盖使用覆盖的 toString() 的 toString()

python - 用户输入后重复 while 循环

java - 调用添加输入的方法