-3

我在运行我的游戏的主 while 循环中有一个开关。我正试图打破我的开关,以便处理不同的情况。下面的示例更好地解释了它:

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
}

}
4

8 回答 8

1

如果您想跳出 for 并直接进入案例 2,那么您的代码应该做到这一点。

于 2013-03-28T16:19:56.787 回答
1

我猜你只想打case 2lives == 0。如果是这样,您需要将您的break;in{ }j = 2;- 放在一起,这样只会在lives == 0. 然后,如果您break;在末尾放置 a ,case 0您将终止开关,然后由于 a 重新进入开关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
  }
}
于 2013-03-28T16:38:17.407 回答
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
于 2013-03-28T19:37:16.407 回答
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
于 2013-04-06T13:32:26.023 回答
0

您无法突破多个可破坏范围,您只能突破最高的范围。

对此草率的“解决方案”是使用 goto 跳转。

真正的解决方案是将你的函数分解成更小的逻辑小函数,然后你可以随意中断或返回。出于代码质量和简洁的目的,您的 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++,但可以轻松应用于其他语言。

于 2013-03-28T16:21:54.637 回答
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.
于 2013-03-28T16:23:11.300 回答
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;
于 2013-03-28T16:24:41.677 回答
0

您的 break 正在脱离 for 循环。因此,您需要添加一些额外的逻辑来检查循环是否提前退出,然后再次调用 break。

其次,for(i = 0; i > 100; i++)永远不会执行,因为 i 永远不会 > 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
}
于 2013-03-28T16:33:32.253 回答