0
//Initialize
i=0;
foreach(runs 8 times) {
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }

  if(what condition do i put here to check if $i changed from the start of the loop?) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
  }
}

やあみんな、これは明らかだと確信していますが、私はこのアルゴリズムに苦労しています。毎回 $i がループの開始時と同じではないことを確認する必要があります。その区別に基づいて、アクション 1 またはアクション 2。

反復 1 では、if($i == 0) { を配置できますが、反復 2 と 3 では失敗する可能性があります。

4

6 に答える 6

0
$ichanged = false;
i=0;
foreach(runs 8 times) {
  //Initialize

  if(some condition that sometimes happens) {
    $ichanged = true;
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $ichanged = true;
    $i = $i -4;
  }

  if($ichanged == false) {
    //nothing changed, do action 1        
  } else {
    //Else something changed and do action 2.  
    $ichanged = false;//RESET for next iteration
  }
}
于 2013-10-15T14:03:01.240 に答える
0

最初の状態を覚えておいて、後で確認してください。

foreach(runs 8 times) {
  //Initialize
  i=0;
  oldi = i;
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }

  if(i != oldi) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
  }
}
于 2013-10-15T13:57:27.733 に答える
0

これを試して:

$previous = 0;
foreach(runs 8 times) {
//Initialize
$i = 0;
if(some condition that sometimes happens) {
    $i = $i + 3;
} else if (some other condition that sometimes happens) {
//Do nothing with i
} else if(some condition that sometimes happens) {
    $i = $i -4;
}


if($i != $previous) {
    //nothing changed, do action 1
} else {
    //Else something changed and do action 2.  
}
$previous = $i;
}
于 2013-10-15T13:57:32.173 に答える
0

簡単です。ループ内にもう 1 つの変数を追加しi、ループが繰り返されるたびに値に初期化するだけです。

このコードを試してください:

//Initialize
i=0;
j=0;
foreach(runs 8 times) {
  j=i;
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }

  if(j==i) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
  }
}
于 2013-10-15T14:11:44.550 に答える
0

$i の値が変更された場合は、別の変数を使用してその値を保存できるはずです。次に、変更を確認するときに、いつでもその変数を $i と比較できます。

ただし、ループ内に $i を含める必要はないと思います。

$previousValue = null;
$i=0;
foreach(runs 8 times) {
  //Initialize
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }

  if($previousValue == $i) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
    $previousValue = $i;
  }
}
于 2013-10-15T13:56:40.827 に答える