我想在一排中找到匹配的硬幣面。我設法讓一行中的3個匹配的尾巴結束循環。如何在一排中找到匹配的硬幣?
但我怎麼能包括頭(它似乎忽略頭)?
例如:
0 is heads
reset
0 is heads
reset
1 is tails
1 is tails
1 is tails
total flips took is: 5
PHP:
$flipCounts = 0;
$matchingFaceTypes = 0;
$targetReached = false;
while ($matchingFaceTypes < 3) {
$faceType = rand(0, 1);
$flipCounts++;
if($faceType == 0) {
$matchingFaceTypes++;
echo $faceType . " is heads ". "\n";
}
if($faceType == 1) {
$matchingFaceTypes++;
echo $faceType." is tails ". "\n";
} else {
$matchingFaceTypes =0;
echo " reset ". "\n";
}
} echo "total flips took is: " . $flipCounts;
你設置'$ matchingFaceTypes = 0;'任何時候'$ faceType == 1'都是錯誤的。你想要做的是跟蹤以前的類型是什麼,然後重置,如果當前類型不同於以前的類型。 – 2014-10-01 17:26:24