Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 隐藏子菜单_Java_Menu_Submenu_Display - Fatal编程技术网

Java 隐藏子菜单

Java 隐藏子菜单,java,menu,submenu,display,Java,Menu,Submenu,Display,在while循环之外尝试使用“exit”(8)if语句时,如何防止子菜单显示?当前,当我输入8时,它显示子菜单,然后显示exitiing,并执行它所在的tester类的其余功能 mainMenu.addOption("Add Item"); // creating menu mainMenu.addOption("Remove Item"); mainMenu.addOption(""); mainMenu.addOption(""); mainMenu

在while循环之外尝试使用“exit”(8)if语句时,如何防止子菜单显示?当前,当我输入8时,它显示子菜单,然后显示exitiing,并执行它所在的tester类的其余功能

    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;
   }
}
import java.util.ArrayList;
导入java.util.Scanner;
/**
显示在控制台上的菜单。
*/
公共类菜单
{
//声明实例变量****
//全球的
私有ArrayList选项;
私人扫描仪;
/**
构造一个没有选项的菜单。
*/
公共菜单()
{
/*构造函数-名称与类名完全匹配的特殊方法。不接受或接受许多参数
*构造函数没有返回类型
*目的是初始化数据变量
*/
选项=新的ArrayList();
in=新扫描仪(System.in);
}
/**
将选项添加到此菜单的末尾。
@param选项要添加的选项
*/
公共void addOption(字符串选项)
{
选项。添加(选项);
}
/**
显示菜单,选项编号从1开始,
并提示用户输入。重复,直到输入有效
是供应的。
@返回用户提供的号码
*/
公共int getInput()
{
int输入;
做
{
对于(int i=0;i选项.size());
返回输入;
}
}

以下代码将首先在
循环期间收集
外部的输入,如果
输入==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");
}

您应该研究
if-then-else
语句及其相对于使用多个
if-then
语句的好处(在某些情况下)。与问题无关如果您仔细研究它,您应该会发现编写程序的另一种方式。您问题的答案是:使用
while
循环,而不是
do while
,并让它检查
是否输入!=8
。无论条件如何,
do while
循环至少迭代一次。这不会导致无限循环吗?我将发布一个答案以更清楚地显示它。