主菜单出现一次,但甚至没有输入退出选项就关闭了。也没有正确调用函数?

Main menu appear once and but closes without even entering exit option. Also doesn't call the function properly?

当我 运行 我的程序使用 "!choice.equals("6")" 时,它 运行 是我的主菜单一次,但不执行以下功能是 selected 并且没有任何反应,立即退出。但是,如果我在 while 循环中的条件是 "choice.equals("6")",它会执行我的功能之一,当我从 1-5 select 甚至没有输入时,主菜单重新出现并退出6个。菜单不会调用 selected 的其他功能。我有点沮丧,因为这两种情况在我正在处理的这个程序中都不起作用。

我使用调试器,当我进入 while 条件时它不会让我通过“!choice.equals(“6”)”。在我决定退出程序之前,我原以为我的主菜单会一直要求我输入一个选项。

private void mainMenu() 
{
    String input = JOptionPane.showInputDialog("Persona Library Main Menu\n" +
             "1. Add from this list:\n" +
             "    -Language\n" +
             "    -Fiction\n" +
             "    -Geography\n" +
             "2. Search Item By ID\n" +
             "3. Remove this from Library\n" +
             "4. Random Books\n" +
             "5. Print complete list or by category\n" +
             "6. Exit Program");
    String choice = input;

        while(!choice.equals("6"));                 
        {

            switch(choice)
            {
            case "1":
                addToLibrary();
                break;
            case "2":
                searchByIDtoPrint();
                break;
            case "3":
                removeFromLibrary();
                break;
            case "4":
                //undecided;
                break;
            case "5":
                searchCategoryToPrint();
                break;
            case "6":
                //undecided
                break;
            }
            input = JOptionPane.showInputDialog("Persona Library Main Menu\n" +
                     "1. Add from this list:\n" +
                     "    -Language\n" +
                     "    -Fiction\n" +
                     "    -Geography\n" +
                     "2. Search Item By ID\n" +
                     "3. Remove this from Library\n" +
                     "4. Random Books\n" +
                     "5. Print complete list or by category\n" +
                     "6. Exit Program");
             choice = input;
        }
}

据我所知 - while(!choice.equals("6")) 条件只会 运行 当 choice 不等于 6 时,这意味着如果你不在您的第一个输入 selection 中输入 6,它将永远不会进入循环并直接转到下一个输入提示。

由于输入提示 不是 循环内部,在您第二次要求他们输入数字后,程序将到达函数的末尾,我假设终止 - 这可能是你的一些问题所在,你没有告诉它在第二次提示后做任何事情。

我可以建议一个 do-while 循环,如:

do { /*Enter input prompts and case statements here*/ } while (program == true)

并且仅在您 select 关闭程序的选项时设置输入。 这样,您保证程序至少 运行 一次,如果您永远不会 select 退出 - 您只需 return 到提示符即可。

*编辑 - 另外,刚刚注意到您从未在 while 循环中更改选择,这意味着您将处于无限循环中。

我希望这能回答你的问题。