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 が false になったときに a をテストする if ステートメントから抜け出したいです。ループで中断して続行するようなものです。何か案は?編集: 申し訳ありませんが、大きな if ステートメントを含めるのを忘れていました。b が false の場合に if(a) から抜け出したいが、if(plot) から抜け出したいわけではない。

4

3 に答える 3

13

ロジックを別のメソッドに抽出できます。これにより、最大 1 レベルの 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 に答える