如何:Java带有退出选项的多个菜单?

时间:2018-07-16 00:23:51

标签: java methods menu

好,所以我的程序有多个菜单。 例如: 你好! 您要不要:
薯条
B哈希棕
问:无,关闭程序

您要不要:
番茄酱
b芥末酱
没有

等等等

因此,我希望在顾客选择调味品后重复菜单1。 此外,我不希望客户选择Q退出程序时是否被询问是否要调味品。
如果客户要退出,如何使程序停止阅读其他菜单?

    if(menuChoice == 'A')
    {
        foodChoice= FRIES;
    }                       
    else
    {
        foodChoice= HASHBROWNS;
    }

我在其他地方有一种验证方法
但基本上您得到的是b或Q,现在A挑炸薯条,其他都挑马铃薯煎饼,然后继续菜单。
我只是不知道最后如何重复此菜单。
我是否将整个内容放入while循环中?

1 个答案:

答案 0 :(得分:0)

下面是完成任务的一种基本方法的示例。在代码中,您可以看到 while 循环的使用。循环使您可以让用户进行其他菜单选择,尤其是在出现错误(例如选择不当)的情况下。它还允许用户从菜单中选择一个以上的项目,直到不再需要为止。

// Declare & initialize a Scanner object
Scanner input = new Scanner(System.in); 

// Will hold menu 1 selection
String menu1Selection = ""; 
// System line separator to use in console writes
String ls = System.lineSeparator(); 
// Will hold all meals ordered by User
String allMeals = "";

// Outer while loop (Main meal selection). This will
// continue to loop (taking orders) until e or E is 
// entered.
while (!menu1Selection.toLowerCase().equals("e")) {
    // Will hold the main meal selected
    String mainMeal = "";
    // Will hold all the condiments selected 
    // for the above main meal.
    String mealCondiments = "";
    // Display the main meal menu...
    System.out.print("Hello! What would you like to order:" + ls
            + "A:\tHamburger" + ls + "B:\tFries" + ls
            + "C:\tTwo Eggs" + ls + "D:\tHashbrowns" + ls
            + "E:\tExit Order System" + ls + "Choice --> ");

    // Get menu selection input from User
    menu1Selection = input.nextLine().toLowerCase();
    // Determine the meal selected based on the
    // menu item letter entered.
    switch (menu1Selection) {
        case "a":
            mainMeal = "Hamburger";
            break;
        case "b":
            mainMeal = "Fries";
            break;
        case "c":
            mainMeal = "Two Eggs";
            break;
        case "d":
            mainMeal = "Hashbrowns";
            break;
        case "e":
            // if e is entered then simply go to the
            // beginning of loop and allow the loop 
            // condition to exit it.
            continue;
        default:
            // If none of the above menu letters were selected 
            // then inform of invalid entry and allow the User 
            // to make a proper selection.
            System.err.println("Invalid menu entry! Try again..." + ls + ls);
            continue;
    }

    // Will hold menu 2 selection
    String menu2Selection = "";
    // Inner while loop for condiments selection. This will
    // continue to loop (adding condiments) until e or E is 
    // entered.
    while (!menu2Selection.toLowerCase().equals("e")) {
        // Display the Condiments Menu
        System.out.print(ls + "What condiments would you like with your "
                + mainMeal + "?" + ls + "A:\tKetchup" + ls
                + "B\tMustard" + ls + "C:\tMayonase" + ls + "D:\t"
                + "Relish" + ls + "E:\tNothing (Done)" + ls + "Choice --> ");

        // Get menu selection input from User
        menu2Selection = input.nextLine().toLowerCase();
        // Will hold the condiment name selected
        String condiments; 
        switch (menu2Selection) {
            case "a":
                condiments = "Ketchup";
                break;
            case "b":
                condiments = "Mustard";
                break;
            case "c":
                condiments = "Mayonase";
                break;
            case "d":
                condiments = "Relish";
                break;
            case "e":
                // if e is entered then simply go to the
                // beginning of loop and allow the loop 
                // condition to exit it.
                continue;
            default:
                // If none of the above menu letters were selected 
                // then inform of invalid entry and allow the User 
                // to make a proper selection.
                System.err.println("Invalid menu entry! Try again..." + ls + ls);
                continue;
        }

        // If a condiment was selected then add it to 
        // the mealCondiments string variable. Delimit
        // with a comma if more than one condiment is
        // being added (Ternary Operator is used for this).
        if (!condiments.equals("")) {
            mealCondiments+= mealCondiments.equals("") ? condiments : ", " + condiments;
        }
    }

    // Display the current meal order
    System.out.println(ls + "===========================================");
    System.out.println("Meal Ordered:\t" + mainMeal + " with " + 
                       mealCondiments);
    System.out.println("===========================================" + ls);
    // Add the ordered meal to the allMeals string variable.
    allMeals+= mainMeal + " with " + mealCondiments + ls;
}

// User has selected e or E to exit the Ordering System.
// Display all meals ordered by User & thank for business.
System.out.println(ls + "===========================================");
System.out.println("Your complete meal order is:" + ls + allMeals);
System.out.println("Thank you for your business. Bye-Bye.");

// Exit Application (Close)
System.exit(0);