简体   繁体   English

JAVA:菜单无限循环

[英]JAVA: Menu Infinite-loop

This is a follow up to a question I have asked previously that did get answers that should have fixed my problem, but unfortunately did not. 这是我之前提出的一个问题的后续问题,该问题确实得到了应该解决我的问题的答案,但不幸的是没有。 My program reads in a text file and organises data before giving the user a number of options. 我的程序读入文本文件并在为用户提供许多选项之前组织数据。 When the program gets to this point I want to user to be able to select an option, that performs an operations, but then returns the user back to the start point to be able to perform more operations. 当程序到达这一点时,我希望用户能够选择执行操作的选项,但随后将用户返回到起点以便能够执行更多操作。 This is the answer I liked best (thanks to Octopus) and am currently trying to implement. 这是我最喜欢的答案(感谢八达通),我正在努力实施。

//set choiceentry to -1, this will make it to enter while loop
 int choiceentry = -1

while(choiceentry < 1 || choiceentry > 3){

        System.out.println("Enter \"1\", \"2\" or \"3\"");
        if(scanchoice.hasNextInt())
        choiceentry = scanchoice.nextInt();

    switch(choiceentry){
        case 1:
           //do logic
           break;
        case 2:
           //do logic
           break;
        case 3:
           //do logic
           break;
    }
}

As I see it, the program should enter the loop initially, allow the user to input a selection, then return back to "enter a value". 在我看来,程序应该最初进入循环,允许用户输入选择,然后返回“输入值”。 However, the program does not return, and terminates after one operation. 但是,程序不会返回,并在一次操作后终止。 How can I prevent this to continue the program running infinitely? 如何防止这种情况继续无限运行?

Thanks in advance! 提前致谢!

The current while loop is there to get valid input -- don't change it. 当前的while循环是为了获得有效的输入 - 不要改变它。

You need to wrap this code in another while loop that loops til a sentinal value is entered. 您需要将此代码包装在另一个while循环中,循环直到输入sentinal值。

while (!isSentinalValue) {
  while (inputNotValid) {
    // get valid input
  }
}

Edit 编辑

More specifically in pseudocode: 更具体地说,在伪代码中:

while (!isSentinalValue) {
  input = invalidValue
  while (inputNotValid) {
     getInput
  }
  use input to do menu things
}

So I would not have the switch block inside of the inner loop, since that loop concerns itself only with making sure that the input entered is valid. 因此,我不会在内部循环内部使用开关块,因为该循环仅关注确保输入的输入有效。 Do the switch block outside of the inner loop, and be sure to set the sentintal value that allows the user to escape the outerloop when appropriate. 切换块在内部循环之外,并确保设置sentintal值,允许用户在适当时逃脱外环。

Your while(choiceentry < 1 || choiceentry > 3) condition is wrong. 你的while(choiceentry < 1 || choiceentry > 3)条件是错误的。 If you want it to loop , then you have to make it between 1 and 3 . 如果你想让它循环,那么你必须在1到3之间。

So this also means that you will have to change your choiceentry initialization value. 所以这也意味着你必须改变你的choiceentry初始化值。 This will work. 这会奏效。

int choiceentry = 1

while(choiceentry >=1 && choiceentry <= 3){

        System.out.println("Enter \"1\", \"2\" or \"3\"");
        if(scanchoice.hasNextInt())
        choiceentry = scanchoice.nextInt();

   ....
}

your loop only runs while choiceentry is less than 1 or greater than 3. As soon as the user enters one of those values, the loop exits. 只有当choiceentry小于1或大于3时,你的循环才会运行。只要用户输入其中一个值,循环就会退出。

Learn to use a debugger. 学习使用调试器。

place the following code after switch 切换后放置以下代码

if(choiceentry == 4){
break;
}

Now when you will input 4 then it will be terminated, you can use any value other then 4 现在,当您输入4然后它将被终止时,您可以使用除4以外的任何值

Use break only when user wants to quit(Say when choiceentry=0). 仅在用户想要退出时使用break(在choiceentry = 0时说)。 You can use "continue" to make loop infinite. 您可以使用“继续”使循环无限。 Sample code is given for reference 示例代码仅供参考

    int choiceentry = 1; // can set any int value except 0 (exit code is 0 for this example)
    Scanner scanchoice = null;

    while (choiceentry != 0) {

        System.out.println("Enter \"1\", \"2\" or \"3\" ..Press 0 to quit");
        scanchoice = new Scanner(System.in);

        if (scanchoice.hasNextInt())
            choiceentry = scanchoice.nextInt();
        // System.out.println("choiceentry=" + choiceentry);

        switch (choiceentry) {

        case 0:

        {
            System.out.println("Bye Bye");
            break;

        }
        case 1:

        {
            System.out.println("In Case 1");
            continue;

        }

        case 2: {
            System.out.println("In Case 2");
            continue;

        }
        case 3: {
            System.out.println("In Case 3");
            continue;

        }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM