-3

基于游戏有多个菜单可供访问,我想添加一个后退按钮或选择。它们都在一个 switch 语句中,例如:

cout<<"1. Open menu 1<<endl<< 2. Open menu 2"<<endl;
int choice;
cin>>choice;
switch(choice){
case 1:
 //Opens menu 1 and shows more text for example:
cout<<"1. Open submenu 1<<endl<< 2. Open submenu 2"<<endl;
int choice2;
cin>>choice2;
switch(choice2){
  //Opens ANOTHER menu and so on and so forth
}
break;

case 2:
 //Opens menu 2 and shows different text
cout<<"1. Open submenu 1<<endl<< 2. Open submenu 2"<<endl;
int choice2;
cin>>choice2;
switch(choice2){
//Opens ANOTHER menu and so on and so forth
}
break;
}


如果用户不小心进入菜单,我如何在这个 switch 语句中制作一种后退按钮?我可以接受不使用开关的选项,我只是想要一个后退选项。谢谢你。

4

1 回答 1

0

我相信这就是您正在寻找的:

main_menu:
cout << "1. Open menu 1" << endl 
     << "2. Open menu 2" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
    //Opens menu 1 and shows more text
    cin >> choice;
    if (choice == 1)
    {
        goto main_menu;
    }
    break;

case 2:
    //Opens menu 2 and shows different text
    cin >> choice;
    if (choice == 1)
    {
        goto main_menu;
    }
    break;
}
于 2021-03-03T14:31:18.473 回答