【发布时间】:2017-10-31 10:10:46
【问题描述】:
所以我正在为我正在学习的网络应用程序开发课程做我的最终项目,并且我正在创建一个强力球彩票生成器。为此,白球号码不能重复。以下是我的代码目前的样子:
<?php
for($x = 1; $x < 6; $x++){
//set each white ball variable (a through e) to a random number between 1 and 69
$a = floor((lcg_value() * 69 + 1));
$b = floor((lcg_value() * 69 + 1));
$c = floor((lcg_value() * 69 + 1));
$d = floor((lcg_value() * 69 + 1));
$e = floor((lcg_value() * 69 + 1));
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
};
?>
此代码的问题是变量“a”到“e”有可能成为重复数字。我可以使用什么代码来确保变量“a”到“e”都不相同?我想过做类似的事情:
if($a != $b || $a != $c || $a || $d...){
//echo numbers
}else{
//generate new numbers
};
但这只是太多的工作,我总是试图找到最有效的方法来编写代码。我不想编写比我需要的更多的代码。任何帮助将不胜感激。先感谢您!
【问题讨论】: