java - 我需要学习如何在案例 6 中返回主菜单,它确认退出,但我不确定它如何循环返回菜单
问题描述
一旦用户输入 6,他们就会看到两个选择,退出程序或返回主菜单。因为我不能使用返回选项,所以我不知道有什么其他方法可以返回菜单
public static void main(String[] args) {
byte choice = 0;
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"This program performs various
computations." +
"\n You will be shown a menu from which" +
"\n you can choose the operations that will be
performed. " +
"\n\n Please click on OK to continue", "Introduction",
JOptionPane.INFORMATION_MESSAGE);
while (choice != 6){
choice = Byte.parseByte(JOptionPane.showInputDialog(null, "
MAIN MENU\n" +
"1. Routines for Triangles\n" +
"2. Routines for Temperatures\n" +
"3. Routines for Geometric Figures\n" +
"4. String Manipulations\n" +
"5. Miscellaneous Simple Games.\n" +
"6. Quit", "Input",JOptionPane.QUESTION_MESSAGE));
switch (choice){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
Object[] quit = {"Yes, exit the program", "No, bring me back
to the menu"};
int option = JOptionPane.showOptionDialog(null,"Are you sure
you want to exit?",
"Choice",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,
quit, quit[0]);
if (option == 0){
System.exit(0);
} //<-- needs to loop back to menu
break;
}
}
}
解决方案
你在评估这个时:
while (choice != 6){}
当您在案例 6 中获得开关时,while 将不再执行。
如果用户决定不退出程序,您可以更改 choise 的值。
if(option !=0){choice=7; break;}
推荐阅读
- android - Androidx EncryptedFile - 将未加密文件写入 EncryptedFile 的最佳方式
- html - 一页上有多个简单的 HTML 播放/暂停按钮的问题
- google-cloud-tasks - Google App Engine Cloud Tasks - 503 请求在等待太长时间以尝试处理您的请求后被中止
- c# - T?,C# 9 中的可空类型参数
- python - 单击鼠标时,如何在 Python Pygame 中制作场景切换器?
- javascript - isModified 函数如何在猫鼬中工作
- redis - 从 GET 返回的 Redigo 类型的值
- python - 如何一次打开 tkinter 和 shell 脚本?
- c# - MongoDB LINQ 查询 - 为用户对象修改更新记录 - 不可变 _id 错误
- c# - 在 C# 应用程序中全局设置算术精度