简体   繁体   English

PHP Do-While循环

[英]PHP Do-While loop

Below is my unfinished code. 下面是我未完成的代码。 The last bit decided to throw a infinite loop. 最后一位决定引发无限循环。 I am trying to create a random number script that will not repeat a number out of the 7 random picks. 我正在尝试创建一个随机数脚本,该脚本不会重复7个随机选择中的数字。 It seems once I added the || 好像我添加了|| on the very last line the script starts to loop. 在最后一行,脚本开始循环。 Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错?

<?php 

$n1 = rand(1, 12);
$n2 = rand(13, 21);
$n3 = rand(15, 27);
$n4 = rand(20, 38); 
$n5 = rand(30, 46);
$n6 = rand(39, 49);
$bo = rand(1, 49);

// First number
echo $n1 . " ";

// Second number
do {
    echo $n2 . " ";
} while ($n1 == $n2);

// Third Number
do {
    echo $n3 . " ";
} while ($n3 == $n2 || $n1);

?>

$n1 is allways different from 0 then $n1始终不同于0

do {
    echo $n3 . " ";
} while ($n3 == $n2 || $n1);  //<-- allways true

What @Majid said was correct. @Majid说的是正确的。 Check PHP's operator precedence . 检查PHP的运算符优先级

The == operator precedes || ==运算符在||之前 operator for execution, so as long as $n1 is not false -y, the loop never exits. 操作符执行,只要$n1不是false -y,循环就永远不会退出。

Not sure what exactly you are trying to do, but your last do while loop is configured incorrectly. 不知道您到底想做什么,但是您上一次的while循环配置错误。 The below is more correct and wont give you an infinite loop. 下面是更正确的,不会给您无限循环。

do {
 $n3 = rand(15, 27);
 echo $n3 . " ";
} while (($n3 == $n2) || ($n3 == $n1));

There are a lot of issues here, but here's a start on the right path. 这里有很多问题,但这是正确道路上的起点。 Comments are inline. 评论是内联的。

<?php 

$n1 = rand(1, 12);
$n2 = rand(13, 21);

// First number
echo $n1 . " ";

// Second number
// no loop needed as your rand doesn't overlap your $n1
echo $n2 . " ";

// Third Number
do {
    $n3 = rand(15, 27); // this needs to be in your loop so when its the same it will generate a new number
} while ($n3 == $n2) // your || isn't needed as it can't over lap your $n2

echo $n3 . " ";

// Fourth Number
do {
    $n4 = rand(20, 38); // this needs to be in your loop so when its the same it will generate a new number
} while ($n4 == $n3)

echo $n4 . " ";

// ... repeat 

// Seventh Number
do {
    $bo = rand(1, 49); // this needs to be in your loop so when its the same it will generate a new number
} while ($bo == $n1 || $bo == $n2 || $bo == $n3 || $bo == $n4 || $bo == $n5 || $bo == $n6) // use your || to check against all of your variables

echo $bo;

?>

yes - Majid is right, this will most always be an infinite loop. 是的-Majid是对的,这通常是一个无限循环。 What you might want is something similar to: 您可能想要的类似于:

$previousNums = array();

for ($i = 0; $i < 7; $i++)
{
  $try = rand(0, 49);
  while (in_array( $try, $previousNums ) ) {
    // means we have already used this num, so try again
    $try = rand(0, 49);
  } 
  // append this num in the list of previousNums
  $previousNums[] = $try;

  echo ' num = ' . $try;
}

The priority of || ||的优先级 is lower than == so actually it is more like this: 小于==,因此实际上更像这样:

while(($n3 == $n2)||$n1))

and I think this is not you want so change it to 我认为这不是您想要的,因此将其更改为

while(($n3 === $n2)||($n3 === $n1))

And please keep in mind to use === for comparing. 并且请记住使用===进行比较。

I hope it helps 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM