I`ve已与PHP玩弄,并试图找到这个“错误”的修复,但没有找到任何关于它...PHP 7 for循环错误if语句。在32.767循环之后停止工作?
任务: 了解需要多少试图发现一个随机选择的号码。
我在for循环之外生成了一个随机数,并声明了一个变量,这个变量将是for循环将尝试猜测这个数字的最大次数。如果在for循环中生成的随机数是在循环外部生成的数字“==”,我将该数字保存在变量中,保存所需的时间以使其正确地跳出循环...简单东东。
“虫”:
Evertything的伟大工程,直到我们得到高达32.767倍里面的for循环。如果我们用少量的工作一切正常,但如果我们尝试一些大的随机数,就像50.000以上一样,for循环中的if语句将在第32.767次执行,无论如何。它就像“==”一样变成了“=”。
Beeing新到PHP我认为这可能是一个记忆的事情,但我尝试了一些大循环而不if语句,他们所有的工作......
下面是代码:
<?php
$number = rand(1,50000); // Change this to something small, bellow 30.000, and it will start to work. make it the same as line 10.
$correctNumber;
$maximumLoops = 50000; // No matter how much loops you try, if the random number is big and we get to the 32.767th loop, the if statement will execute.
$numberOfLoops = 0;
for($i = 0; $i < $maximumLoops; $i++){
$randNumber = rand(1,50000); // Change this to something small, bellow 30.000, and it will start to work. make it the same as line 3.
if($randNumber == $number){
$correctNumber = $number;
$numberOfLoops = $i;
break;
}
}
echo "The Number was: $number<br>";
echo "Correct Number is: $correctNumber<br>";
echo "The maximum number of loops was: $maximumLoops<br>";
echo "Number of loops to get it right: $numberOfLoops";
任何帮助将不胜感激。由于
这是你使用出示你的“错误”确切的代码问题上运行?它正在处理我的问题,我得到了一些类似于'循环的数目:47520' – morcen