PHP初学者在这里,我试图使用下面的代码生成5个随机浮点数,然后将它们一起添加。但是,继续运行“无法重新声明”错误。任何帮助或建议?谢谢!
function RandRoll($mod1, $mod2){
$modCalc = 1.00 + $mod1 + $mod2;
echo "(" . $modCalc . ")";
$RAND_MIN = $modCalc;
$RAND_MAX = 100.00;
function frand($min, $max, $decimals = 0) {
$scale = pow(10, $decimals);
return mt_rand($min * $scale, $max * $scale) / $scale;
}
$result = frand($RAND_MIN, $RAND_MAX, 2);
return $result;
}
$moda1 = 23.45;
$moda2 = 17.21;
$score1 = RandRoll($moda1, $moda2);
$score2 = RandRoll($moda1, $moda2);
$score3 = RandRoll($moda1, $moda2);
$score4 = RandRoll($moda1, $moda2);
$score5 = RandRoll($moda1, $moda2);
echo $score1 + $score2 + $score3 + $score4 + $score5;
答案 0 :(得分:2)
正确的缩进可帮助您调试代码
您已在函数中声明了一个函数,因此每次运行外部函数时都会尝试重新声明内部函数
将代码修改为
function frand($min, $max, $decimals = 0) {
$scale = pow(10, $decimals);
return mt_rand($min * $scale, $max * $scale) / $scale;
}
function RandRoll($mod1, $mod2){
$modCalc = 1.00 + $mod1 + $mod2;
echo "(" . $modCalc . ")";
$RAND_MIN = $modCalc;
$RAND_MAX = 100.00;
$result = frand($RAND_MIN, $RAND_MAX, 2);
return $result;
}