我的PHP计算器需要帮助。
我当前的输出:
我想要的输出:
所以我需要直列,输出正确的顺序等
我的代码:
function calc($operator, $num)
{
if ($num < 0) {
return 'Number must be more than zero';
}
$operatorsMap = ['+', '-', '*', '/'];
if (!in_array($operator, $operatorsMap)) {
return 'You have entered a bad operator';
}
for ($rows = 0; $rows <= $num; $rows++) {
echo "\n|";
for ($columns = 0; $columns <= $num; $columns++) {
switch ($operator) {
case '+':
echo round($columns + $rows, 2);
echo "|";
break;
case '-':
echo round($columns - $rows, 2);
echo "|";
break;
case '*':
echo round($columns * $rows, 2);
echo "|";
break;
case '/':
echo round($columns / $rows, 2);
echo "|";
break;
}
}
}
}