首页 > 解决方案 > 在用户回答“是”后尝试让程序返回主菜单以继续,或者如果用户回答“否”,则程序结束并显示总计消息

问题描述

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。对 true 和 false 使用布尔值。但没有运气。谢谢任何可以为我解决这个问题的人。

标签: javamethodswhile-loopresetcontinue

解决方案


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

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

PS 我很快查看了您的其余代码并添加了一些额外的注释,这可能会帮助您改进它。祝你学习 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
        }
    }
}

推荐阅读