我想重申一個程序,直到$頭變量和$尾變量都大於0.我無法弄清楚我做錯了什麼。 while循環經過一次迭代後總會被破壞。我想要它之前迭代打破
<?php
echo "<table border=\"1\">";
echo "<tr><td>Person</td><td>Heads</td><td>Tails</td><td>Total</td></tr>";
for ($person=1; $person < 11; $person++){
echo "<tr><td>Person $person </td>";
$both = 0;
$heads = 0;
$tails = 0;
$total = 0;
while ($both < 1){
do {
$total++;
$random = rand(1,2);
if ($random == 1){
$heads++;
} else{
$tails++;
}
} while (($tails < 0) && ($heads < 0));
$both = 1;
}
echo "<td>$heads</td><td>$tails</td><td>$total</td>";
echo "</tr>";
}
echo "</table>";
?>
工作很好。非常感謝。我不知道爲什麼我認爲這是&&操作符! – stytown
對於你的問題,在$ tail和$ heads都大於0後,它只賦值$ both = 1。 – stytown
@stytown對,但是你不會退出這個內部循環,直到'$ tails'和'$ heads'無論如何,大於'0',所以你永遠不會多次運行外部循環。 – Wiseguy