dongmin1166 2016-10-20 08:05
浏览 58

在5行以及2列表生成器上遇到php Random Math和operator generator问题

So far I have this to generate random numbers, and I need it to generate random operators, in which it just scrolls through the array and gives off the same 2 operators.

So instead of generating 5 lines it generates about 12. I can get it to show the operator in the math problem but I cannot get the operators to randomize on each line. Something is wrong somewhere.

I also have to have the problem generate in two column table where it has problem in one column and solution in the other. If someone can at least point me in the right direction it would be much appreciated!

<?php
for ($x = 0; $x <= 5; $x++) {
    $num1 = mt_rand(1, 9);
    $num2 = mt_rand(1, 9);

    $operators = array("+","-",);

    switch ($operators[array_rand($operators)]) {
        case "+":
            $result = $num1 + $num2;
            break;
        case "-":
            $result = $num1 - $num2;
            break;
    }
    foreach($operators as $value){
        echo "$num1 $value $num2 = $result <br>";
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dongpu1881 2016-10-20 08:16
    关注

    Try this way:

    for ($x = 0; $x <= 5; $x++) {
    $num1 = mt_rand(1, 9);
    $num2 = mt_rand(1, 9);
    
    $operators = array("+","-",);
    $value = $operators[array_rand($operators)];
    
    switch ($value) {
    case "+":
        $result = $num1 + $num2;
        break;
    case "-":
        $result = $num1 - $num2;
        break;
    }
        echo "$num1 $value $num2 = $result <br>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效