隐藏子菜单

时间:2016-02-22 23:01:09

标签: java menu submenu display

当我尝试使用"退出"时,如何防止子菜单显示? (8)while循环之外的if语句?目前,当我输入8时,它会显示子菜单,然后显示exitiing,并执行它所在的测试器类的其余功能。

    mainMenu.addOption("Add Item");  // creating menu
    mainMenu.addOption("Remove Item");
    mainMenu.addOption("");
    mainMenu.addOption("");
    mainMenu.addOption("");
    mainMenu.addOption("");
    mainMenu.addOption("");
    mainMenu.addOption("Exit");

    subMenu.addOption("Burger");
    subMenu.addOption("Drink");
    subMenu.addOption("Fries");

    int input;
    int choice;
    do{
        input = mainMenu.getInput();
        choice = subMenu.getInput();
        if(input == 1){
            if(choice == 1){
                System.out.println("Burger Added");
            }
            if(choice == 2){
                System.out.println("Drink Added");
            }
            if(choice == 3){
                System.out.println("Fries Added");
            }
        }
        if(input == 2){
            if(choice == 1){
                System.out.println("Burger Removed");
            }
            if(choice == 2){
                System.out.println("Drink Removed");
            }
            if(choice == 3){
                System.out.println("Fries Removed");
            }
        }
    } while(input != 8);
    if(input == 8){
        //choice = 8;
        System.out.println("exiting");
    }

这是菜单类

import java.util.ArrayList;
import java.util.Scanner;

/**
    A menu that is displayed on a console.
 */
public class Menu
{
    // Declare instance variables ****
    // Global
    private ArrayList<String> options;


private Scanner in;

   /**
      Constructs a menu with no options.
   */
   public Menu()
   {
       /* Constructor - a special method whose name matches the class name exactly. Takes no or many arguments
       *  no return type for constructors
       *  purpose is to initialize data variables
       */
      options = new ArrayList<String>();
      in = new Scanner(System.in);
   }


   /**
      Adds an option to the end of this menu.
      @param option the option to add
   */
   public void addOption(String option)
   {
      options.add(option);
   }

   /**
      Displays the menu, with options numbered starting with 1,
      and prompts the user for input. Repeats until a valid input
      is supplied.
      @return the number that the user supplied
   */
   public int getInput()
   {
      int input;
      do
      {
         for (int i = 0; i < options.size(); i++)
         {
            int choice = i + 1;
            System.out.println(choice + ") " + options.get(i));
         }
         input = in.nextInt();
      }
      while (input < 1 || input > options.size());
      return input;
   }
}

1 个答案:

答案 0 :(得分:0)

以下代码将首先收集while循环之外的输入,如果input == 8则它将退出,如果不是则通过子菜单。最后再次收集输入。

input = mainMenu.getInput();

while (input != 8) {

    choice = subMenu.getInput();

    if (input == 1) {
        if (choice == 1) {
            System.out.println("Burger Added");
        } else if (choice == 2) {
            System.out.println("Drink Added");
        } else if (choice == 3) {
            System.out.println("Fries Added");
        }
    } else if (input == 2) {
        if (choice == 1) {
            System.out.println("Burger Removed");
        } else if (choice == 2) {
            System.out.println("Drink Removed");
        } else if (choice == 3) {
            System.out.println("Fries Removed");
        }
    }

    input = mainMenu.getInput();
}

if (input == 8) {
    System.out.println("exiting");
}

下一个do-while循环将收集循环内部的输入,并在input == 8时退出。

do {
    input = mainMenu.getInput();

    if (input != 8) {
        choice = subMenu.getInput();

        if (input == 1) {
            if (choice == 1) {
                System.out.println("Burger Added");
            } else if (choice == 2) {
                System.out.println("Drink Added");
            } else if (choice == 3) {
                System.out.println("Fries Added");
            }
        } else if (input == 2) {
            if (choice == 1) {
                System.out.println("Burger Removed");
            } else if (choice == 2) {
                System.out.println("Drink Removed");
            } else if (choice == 3) {
                System.out.println("Fries Removed");
            }
        }
    }

} while (input != 8);

if (input == 8) {
    System.out.println("exiting");
}