我想让这个函数生成5次随机数。我已经尝试使用do/while循环,但它不起作用:
function myfunction(): int {
$i = 0;
do {
$k = mt_rand(10000, 99999);
$i++;
} while($i <= 4);
{
return $k;
}
}发布于 2018-08-13 23:29:35
将值堆叠在数组中,在do-while循环中,在函数结束之前添加数组的返回值。
如下所示:
function myfunction():array{
$randomNumbers = array();
$i=0;
do{
$k=mt_rand(10000,99999);
$i++;
$randomNumbers[] = $k;
} while($i<=4) ;
return $randomNumbers;
}
print_r(myfunction());发布于 2018-08-13 23:23:50
do while循环的语法如下...
do {
code to be executed;
} while (condition is true);你必须更新你的代码,如下所示……
do{
$k=mt_rand(10000,99999);
$i++;
}while($i<=4);发布于 2018-08-14 02:32:41
在发送给你之前,我已经对代码做了一些修改和测试。请检查以下代码:
function fiveRandomNos(){
$rno = array();
$count=0;
while($count <= 4){
$rno[]=mt_rand(10000,99999);
$count++;
}
return $rno;
}
print_r(fiveRandomNos()) ;https://stackoverflow.com/questions/51825520
复制相似问题