6

我有这样的东西

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
}

我想打破当 b 变为假时测试 a 的 if 语句。有点像循环中断和继续。有任何想法吗?编辑:抱歉忘记包含大 if 语句。我想在 b 为假时突破 if(a) 但不突破 if(plot)。

4

3 回答 3

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   
}
于 2013-07-18T15:37:21.683 回答
7
if(plot)
{
    if(a)
    {
        b= !b;
        if( b )
        {
            //do something meaningful stuff here
        }
    }
    //some more stuff here that needs to be executed
}
于 2013-07-18T15:37:08.137 回答
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
  }
}

这应该做你想要的..

于 2013-07-18T15:29:25.930 回答