C++ 如何在 case 之后循环回到 switch 语句的开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13089374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to loop back to the beginning of a switch statement after a case
提问by Vladimir Marenus
I have what I'm sure is a simple problem, but I can't figure it out. In the code below, I'd like to be able to have case 5 re-display the options afterward. How can I do this? Thanks in advance!
我有一个我确定是一个简单的问题,但我无法弄清楚。在下面的代码中,我希望能够让案例 5 之后重新显示选项。我怎样才能做到这一点?提前致谢!
// Input the race of your character
cout << "Choose a race here: " << endl
<< "1) Human, 2) Elf, 3) Dark Dwarf, 4) Commoner, 5) Race info, 6) Admin Debug Race : ";
cin >> mCharRace;
switch (mCharRace)
{
case 1:
cout << "You have chosen Human!" << endl;
mExpPoints = 999;
mArmor = mArmor + 2;
break;
case 2:
cout << "You have chosen Elf!" << endl;
mAccuracy = mAccuracy + 2;
mWeapon.mDamageRange.mLow = mWeapon.mDamageRange.mLow + 1;
break;
case 3:
cout << "You have chosen Dark Dwarf!" << endl;
mWeapon.mDamageRange.mHigh = mWeapon.mDamageRange.mHigh + 2;
mMaxHitPoints = mMaxHitPoints + 3;
break;
case 4:
cout << "You have chosen Commoner! Brave man." << endl;
mAccuracy = mAccuracy - 3;
mHitPoints = mHitPoints - 5;
mMaxHitPoints = 8;
mExpPoints = -250;
mNextLevelExp = 1500;
mArmor = -1;
break;
case 5:
cout << "Placeholder for explanation text." << endl;
break;
case 6:
cout << "ADMIN POWERS UNITE!!!!!!!" << endl;
mAccuracy = 20;
mHitPoints = 1000;
mMaxHitPoints = 1000;
mExpPoints = 0;
mNextLevelExp = 1000;
mArmor = 100;
mWeapon.mName = "Admin Sword of HNNNG!";
mWeapon.mDamageRange.mLow = 100;
mWeapon.mDamageRange.mHigh = 150;
mGold = 1000000;
break;
回答by Anirudh Ramanathan
A switch-case construct isn't an iteration construct.It can only select a certain case and execute it. The flow of control cannot go back up due to it.
switch-case 构造不是迭代构造。它只能选择某个案例并执行它。由于它,控制流无法恢复。
So, you can't use it to loop. Wrap it in a whileor forlooping construct instead.
所以,你不能用它来循环。将其包装在一段时间或for循环构造中。
while(condition){
switch (mCharRace){
...
...
}
}
Just turn conditionto falsewhen you want to stop looping.
当您想停止循环时,只需将条件设置为false。
回答by LSerni
Wrap the input and switch code in a loop, and assign a variable exitLoop
to exit the loop. Set that variable to TRUE by default, except in those cases where you want to repeat the input. In those, set exitLoop
to false.
将输入和开关代码包装在一个循环中,并分配一个变量exitLoop
以退出循环。默认情况下将该变量设置为 TRUE,除非在您想要重复输入的情况下。在那些中,设置exitLoop
为false。
回答by cjamesm
Add a label before your first cout
在第一次出价前添加标签
label:
cout << "Choose a race here: " << endl
<< "1) Human, 2) Elf, 3) Dark Dwarf, 4) Commoner, 5) Race info, 6) Admin Debug Race : ";
cin >> mCharRace;
...
then inside the case 5, add
然后在案例 5 中,添加
goto label;
回答by user207421
Put it inside a for(;;)
loop and use continue
instead of break
. Put another break
after the end of the switch
statement to catch the cases that really do want to break:
把它放在一个for(;;)
循环中并使用continue
而不是break
. break
在switch
语句结束后放置另一个以捕获真正想要中断的情况:
for (;;)
{
switch (x)
{
case case_that_wants_to_loop:
// ...
continue;
case case_that_wants_to_break:
// ...
break;
}
break;
}