我在主循环中有一个开关,它将运行我的游戏。我试图打破我的开关,以便转到另一种情况。以下示例更好地解释了它:
int j = 0;
While(1){
switch(j){
case 0: ....
break;
case 1:
for( i =0; i > 100; i++){
if(lives == 0)
j = 2;
break; //this is where I want to break out of my switch to go to case 2. But it
//breaks out of my for loop. I do not know how to get around this. Thank
//you!
}
case 2: //some stuff I want to do
}
}
答案 0 :(得分:1)
如果您想要突破并直接进入案例2,那么您的代码应该完全正确。
答案 1 :(得分:1)
我想你只想在case 2
时点击lives == 0
。如果是这样,您需要将break;
与{ }
放在j = 2;
中 - 这样只会在lives == 0
时打破循环。然后,如果您在break;
末尾放置case 0
,则会终止切换,然后由于while(1)
重新进入切换,然后点击case 2:
。
你的for循环应该是for(i = 0; i < 100; i++)
而不是for(i = 0; i > 100; i++)
int j = 0;
While(1){
switch(j){
case 0: ....
break;
case 1:
for( i =0; i < 100; i++){
if(lives == 0) {
j = 2;
break;
}
}
break;
case 2: //some stuff I want to do
}
}
答案 2 :(得分:1)
好的,你实际上是在正确的轨道上,但是,如上所述,你需要检查你的循环条件。
int j = 0;
While(1){
switch(j){
case 0: ....
break;
case 1:
for( i =0; i < 100; i++){
if(lives == 0){
j = 2;
break; // Break for
}
}
if(j != 2) break; // If no break in the switch's case, then continue to the next case
case 2: //some stuff I want to do
break;
} //End Switch
} //End While
答案 3 :(得分:1)
构建这样的状态机:
C# bool OnceMore = true;
string ProcessState = "Start"
while(OnceMore)
{
OnceMore = False;
switch(ProcessState)
{
case "Start" :
{
// do something
ProcessState = "Check";
OnceMore = True;
break;
}
case "Check" :
{
// do check
if( Check == "Ok")
{
ProcessState = "Close";
}
else // try again
{
ProcessState = "Start";
}
OnceMore = True;
break;
}
case "Close" :
{
// do what you need to return to the caller
break;
}
}
}
return
答案 4 :(得分:0)
你不能突破多个可破坏的范围,你只能突破最高的范围。
对此的草率“解决方案”是使用goto jump。
真正的解决方案是将您的功能分解为更小的逻辑一口大小的功能,然后您可以随意中断或返回。为了代码质量和清洁目的,你的for循环几乎肯定应该在它自己的功能中。
bool doSomethingInALoopWithADescriptiveName(int &var)
{
for(int i = 0; i < 100; i++)
{
if(lives_WhichShouldntBeGlobal == 0)
{
var = 2;
return false;
}
}
return true;
}
void myFuncWithADescriptiveName()
{
int variableWithADescriptiveName = 0;
while(true)
{
switch(j)
{
case 0:
{
//....
} break;
case 1:
{
bool continueLooping = doSomethingInALoopWithADescriptiveName(&variableWithADescriptiveName);
if(!continueLooping)
{
return;
}
} break;
case 2:
{
//some stuff I want to do
} break;
}
}
}
注意:由于问题的标记问题,我不确定问题的语言是什么。我的答案适用于多种语言(可以概括为“编写简单,紧凑,自我描述和干净的代码”),但我的代码示例是伪C ++,但很容易适用于其他语言。
答案 5 :(得分:0)
switch(j){
case 0: ....
break test;
case 1:
for( i =0; i > 100; i++){
if(lives == 0)
j = 2;
break; //this is where I want to break out of my switch to go to case 2. But it
//breaks out of my for loop. I do not know how to get around this. Thank
//you!
}
break test;
case 2: //some stuff I want to do
break test;
}
test://After switch.
答案 6 :(得分:0)
将大括号括在触发下一个案例的条件
case 1:
for( i =0; i > 100; i++)
{
if(lives == 0)
{
j = 2;
break; // This exits the for effectively jumping at the case break
}
// I suppose here you have other code to process
// that should not be executed if (lives == 0)
}
break;
答案 7 :(得分:0)
你的休息时间突破了for循环。因此,您需要添加一些额外的逻辑来检查循环是否提前退出,然后再次调用break。
其次,对于(i = 0; i> 100; i ++)永远不会执行,因为我永远不会&gt; 100。
for(int i = 0; i < 100; i++)
{
if(lives == 0)
{
j = 2;
quit = true;
break; //Break out of the loop
}
}
//Note, if this language is C, control will just fall through to case 2 anyway.
//If C#, you have to add a break at the end of this case regardless
if(quit == true)
{
break; //Break out of switch
}