1

我有一个网站需要根据 3 个参数生成随机数学问题:运算符(加法、减法、乘法、除法)、重组(进位或不进位)和列(一或二)。我一直在努力思考如何去做这件事,但我想出的每件事都在某种程度上存在缺陷。

这是我目前正在使用的内容:

function create_problem($operator, $regrouping, $columns){
    $top = rand(1,9);
    $bottom = rand(1,9);

    if($operator == "+"){
        if($columns == 1 && $regrouping === false){
            $result = array(
                'top' => $top,
                'bottom' => $bottom,
                'formula' => "$top.$operator.$bottom"
            );
        }
        if($columns == 2){
            if($regrouping === false){
                if($top+$bottom > 9){
                    $diff = ($top+$bottom)-10;
                    $top = $diff+rand(1, 3);    
                }
                $result = array(
                    'top' => $top,
                    'bottom' => $bottom,
                    'formula' => "$top.$operator.$bottom"
                );
            }else{
                if($top+$bottom < 10){
                    $top = rand(1, $bottom+1);
                }
            }
        }
    }
    return $result;
}

如果有人处理过这个问题,或者如果有人有任何指示,我将不胜感激!

4

1 回答 1

2

此函数首先检查重组,并生成随机数,使公共列数的总和小于 10(对于无进位)或大于或等于 10(对于进位)。

function create_problem($operator, $regrouping, $columns){
$x1 = '';
$x2 = '';
$y1 = '';
$y2 = '';
if ($regrouping){
    if ($columns == 2){
    $x1 = rand(1,9);
    $x2 = rand(0,9);
    $y1 = rand(9-$x1,9);
    $y2 = rand(10-$x2,9);
    } else {
    $x1 = rand(1,9);
    $y1 = rand(9-$x1,9);
    }
} else {
    if ($columns == 2){
    $x1 = rand(1,8);
    $x2 = rand(0,8);
    $y1 = rand(1,9-$x1);
    $y2 = rand(0,9-$x2);
    } else {
    $x1 = rand(1,8);
    $y1 = rand(1,9-$x1);
    }
}

return $x1.$x2.$operator.$y1.$y2;
}

前任..

31 +54

..其中 3=$x1 1=$x2 5=$y1 4=$y2

于 2012-12-09T06:37:23.510 回答