我在PHP中创建一个乘法表系统。
此系统的输入是表格编号和问题数量。 跳过表1,因此输入表格
此时我只是填写这样的数组:
$nCount = $_POST['count'];
$nHighest = $_POST['table'];
$aSums = [];
$nCounter = 0;
while($nCount > 0){
$cSumString = rand(2, $nHighest) . "*" . rand(1, 10);
$aSums[$nCounter] = $cSumString
$nCount--;
$nCounter++;
}
我想以多种方式解析问题:
3 * 5 = ... (normal)
... * 5 = 25 (first number to fill in)
8 * ... = 16 (second to fill in)
这需要随机化。
示例:
1 * 2 = ...
6 * ... = 12
... * 4 = 20
8 * 4 = ...
2 * ... = 6
我唯一知道的应该是这样的,所以这是一个开关,但我似乎无法让它正常工作,所以如果有人能给我一个正确的方向,我会很感激。我不是要求instacode只是一些提示会很棒。
答案 0 :(得分:1)
<?php
$total = 4;
$table = 7;
$generate_pair = function() use ($table)
{
$first = rand(1, 10);
$last = rand(2, $table);
return [$first, $last];
};
$pairs = [];
while (count($pairs) < $total) {
$pair = $generate_pair();
if(!in_array($pair, $pairs))
$pairs[] = $pair;
}
$questions = array_map(function ($v) {
return sprintf('%d * %d', $v[0], $v[1]);
}, $pairs);
var_dump($questions);
示例输出:
array(4) {
[0]=>
string(5) "7 * 3"
[1]=>
string(5) "5 * 7"
[2]=>
string(5) "5 * 2"
[3]=>
string(5) "6 * 2"
}
答案 1 :(得分:1)
$nCount = $_POST['count'];
$nHighest = $_POST['table'];
$aSums = [];
$leftOut = ['leftFactor', 'rightFactor', 'product'];
for ($i = 0; $i <= $nCount; $i++) {
$leftOutRand = rand(0, count($leftOut) - 1);
$factor = rand(1, $nHighest);
$product = rand(0, $nHighest) * $factor;
switch($leftOut[$leftOutRand]) {
case 'leftFactor':
$cSumString = '...' . ' * ' . $factor . ' = ' . $product;
break;
case 'rightFactor':
$cSumString = $factor . ' * ' . '...' . ' = ' . $product;
break;
case 'product':
$cSumString = rand(1, $nHighest) . ' * ' . rand(1, $nHighest) . ' = ' . '...';
break;
}
$aSums[$i] = $cSumString;
}
示例输出:
2 * ... = 4
... * 2 = 6
1 * 1 = ...
... * 2 = 6
3 * ... = 12
2 * ... = 2
2 * 4 = ...
4 * ... = 0
3 * 1 = ...
2 * 1 = ...
... * 4 = 16
1 * ... = 2
... * 2 = 2
1 * 4 = ...
3 * ... = 9
2 * 3 = ...
2 * ... = 6
... * 1 = 3
... * 4 = 4
1 * 1 = ...
... * 3 = 9
答案 2 :(得分:0)
我个人把它放在一个数组中并从那里读取然后通过像Mark labenski提到的开关更容易得到它,我会这样做:
$questpertable = floor($questions / ($table-1));
$nTableCounter = 2;
$array = [];
for ($nX = 0; $nX < ($table-1); $nX++){
for ($nY = 0; $nY < $questpertable;){
$left = rand(1, 10);
$right = $tablecounter;
$answer = $left * $right;
$array[$nY] = array($left, $right, $answer );
$Y++;
}
$tablecounter++;
}
唯一遗漏的是剩余价值,你必须找到解决这个问题的东西。