如何从if语句中的布尔值中取出if语句

Chr*_*ris 6 .net c# if-statement

我有类似的东西

bool a = true;
bool b = true;
bool plot = true;
if(plot)
{
    if(a)
    {
        if(b)
            b = false;
        else
            b = true;
    //do some meaningful stuff here
    }
//some more stuff here that needs to be executed
}
Run Code Online (Sandbox Code Playgroud)

我想打破if测试b何时变为false的语句.有点喜欢休息并继续循环.有任何想法吗?编辑:抱歉忘了包含大if语句.我想打破if(a)当b是假的但是没有突破if(plot).

Ser*_*kiy 13

您可以将逻辑提取到单独的方法中.这将允许您具有最大一级ifs:

private void Foo()
{
   bool a = true;
   bool b = true;
   bool plot = true;

   if (!plot)
      return;

   if (a)
   {
      b = !b;
      //do something meaningful stuff here
   }

   //some more stuff here that needs to be executed   
}
Run Code Online (Sandbox Code Playgroud)

  • +1:这样更加清晰. (4认同)

Den*_*ore 7

if(plot)
{
    if(a)
    {
        b= !b;
        if( b )
        {
            //do something meaningful stuff here
        }
    }
    //some more stuff here that needs to be executed
}
Run Code Online (Sandbox Code Playgroud)

  • 如果筑巢需要更多! (19认同)
  • 是从原来的最小变化. (2认同)

dot*_*ixx 5

bool a = true;
bool b = true;
bool plot = true;
if(plot && a)
{
  if (b)
    b = false
  else
    b = true;

  if (b)
  {
    //some more stuff here that needs to be executed
  }
}
Run Code Online (Sandbox Code Playgroud)

这应该做你想要的..

  • 伙计们,你在开玩笑吧?这与所讨论的逻辑不同 (6认同)
  • +1用于解密问题 (4认同)