时间:2021-07-01 10:21:17 帮助过:25人阅读
optTimes + 1; $index++) { $elements[] = $this->randomValue(); } $operatorType = strlen($this->operators); //有几个运算符供选择 //开始组装算式 $question = ''; for ($index = 0; $index < count($elements); $index++) { $question.=' ' . $elements[$index] . ' '; //放一个数字进来 if ($index < count($elements) - 1)//如果不是最后一个数字的话,在后面加个运算符 $question.=substr($this->operators, mt_rand(0, $operatorType - 1), 1); } eval('$anwser = ' . $question . ';'); if ($anwser < 0) { //排除结果为负数的情况 $elements = array(); goto start; //需PHP5.3的支持 } echo "$question= " . $anwser; } /** * 产生一个范围内的随机值 * * @return int */ protected function randomValue() { return mt_rand($this->scope[0], $this->scope[1]); } }
测试
generate(); ?> 结果:26 + 85 = 111
http://www.bkjia.com/PHPjc/440112.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/440112.htmlTechArticle?php/** * * 出题引擎一枚 * * @author Tourmaline * @copyright (c) 2013, Unary Inc. */class QuestionEngine { /** * 出题范围 * @var string $scope */ public $scope = array(1,...