我有5个案例的开关案例,每个案例都有不同的任务要做。我希望这样做,以便当用户选择案例的选项并且任务由程序完成时,菜单再次出现并且用户可以继续从菜单中选择某些内容,直到用户选择退出的菜单选项5。
到目前为止,我有://sys.out to print the menu options
System.out.print("Enter the number of your choosing: ");
menu = scan.nextInt();
System.out.println();
switch (menu) {
case 1:
//code
break;
case 2:
//code
break;
case 3:
//code
break;
case 4:
//code
break;
case 5:
//code
break;
case 12:
System.exit(0);
break;
}
答案 0 :(得分:1)
怎么样:
int menu = 0;
//you already said it: "the user can keep choosing something from
//the menu until the user choses menu option 5"
while(menu != 5) {
System.out.print("Enter the number of your choosing: ");
menu = scan.nextInt();
System.out.println();
switch(menu){
case 1: ...
...
case 5:
break;
default: System.out.println("Not supported.");
break;
}
}
//exit...
答案 1 :(得分:0)
public int getItem ()
{
int item = 0;
do
{
System.out.println("Enter");
item = scanner.nextInt();
scanner.nextLine();
}
while (item < 0 || item > 5);
return item;
}
public void start ()
{
int item;
show ();
item = getItem();
switch (item)
{
case 1: code break;
.......
case 5: code break;
}
while (item !=0)
System.out.println("buye");