Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$opt = $_GET['opt'];
$result = 0;
if(!is_numeric($num1)){
$result = '请输入数字1';
}else if(!is_numeric($num2)){
$result = '请输入数字2';
}else{
switch($opt){
case '+':
$result = $num1 + $num2;
break;
case '-':
$result = $num1 - $num2;
break;
case '*':
$result = $num1 * $num2;
break;
case '/':
if($num2 == '0'){
$result = '除数不能为0 !';
}else
{
$result = $num1 / $num2;
}
break;
case '%':
if($num2 == '0'){
$result = '除数不能为0 !';
}else
{
$result = $num1 % $num2;
}
break;
default:
$result = '无效运算!';
};
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="get" style="width:20em;display:grid;grid-template-columns:30% 70%;gap:1em;">
<label>数字1:</label><input type="number" name="num1" value="<?= isset($_GET['num1'])?$num1 :'' ?>">
<label>运算符:</label>
<select name="opt" id="">
<option value="+" <?= ($opt == '+') ? 'selected':'' ?> >+</option>
<option value="-" <?= ($opt == '-') ? 'selected':'' ?> >-</option>
<option value="*" <?= ($opt == '*') ? 'selected':'' ?> >*</option>
<option value="/" <?= ($opt == '/') ? 'selected':'' ?> >/</option>
<option value="%" <?= ($opt == '%') ? 'selected':'' ?> >%</option>
</select>
<label>数字2:</label><input type="number" name="num2" value="<?= isset($_GET['num2']) ?$num2 : '' ?>">
<label>结 果:</label><input type="text" value = "<?= $result ?>">
<label></label><input type="submit" value="提交">
</form>
</body>
</html>
运行结果: