因此,我正在编写一个基于菜单的程序,并且我陷入了一个部分。这里是我的代码:通过switch语句循环
public static void main(String [] args) throws FileNotFoundException {
switch (menu()) {
case 1:
System.out.println("Stub 1");
menu();
break;
case 2:
System.out.println("Stub 2");
menu();
break;
case 3:
System.out.println("Stub 3");
menu();
break;
case 4:
System.out.println("Program Terminated");
break;
}
}
public static int menu() {
System.out.println("Choose a task number from the following: ");
System.out.println("\t1. - See histogram of name's popularity");
System.out.println("\t2. - Compare two names in a specific decade");
System.out.println("\t3. - Show what name had a specific rank for a certain decade");
System.out.println("\t4. - Exit program");
int opt = 0;
int option = getInt(input,"Enter number (1-4): ", 1, 4);
if (option == 1) {
opt = 1;
}
else if (option == 2) {
opt = 2;
}
else if (option == 3) {
opt = 3;
}
else {
opt = 4;
}
return opt;
}
我的问题是,我怎么能得到菜单'按钮后重置'重置'。例如,我选择1,程序执行该操作,完成后,它会再次显示选项菜单,直到按4来终止它。
1和4
尝试从每个案例子块中删除菜单()调用。四是唯一一个不再叫它,这可能是为什么它打破了。 – markg 2014-11-22 02:14:38
这只是使它运行一次@markg – Zero 2014-11-22 02:17:11
也许这个链接可以帮助你:http://stackoverflow.com/a/17015039/2444047 – Splamy 2014-11-22 02:17:57