jeudi 20 octobre 2016

Having trouble with php Random Math and operator generator on 5 line as well as 2 column table 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 just started php but for some reason I cannot get this at all what so ever. 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>";
  }
}

?




Aucun commentaire:

Enregistrer un commentaire