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

时间:2013-07-18 15:25:32

标签: c# .net 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
}

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

3 个答案:

答案 0 :(得分: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   
}

答案 1 :(得分:7)

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

答案 2 :(得分: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
  }
}

这应该做你想要的......

相关问题