老师帮忙检查一下

来源:1-17 编程练习

Dante丨R

2019-03-04 13:30:25

<!DOCTYPE html>

<html lang="en">

<head>

<title>Document</title>

</head>

<body>

<form action="#" method="get">

数值1:

<input type="text" name="num1"/>

<select name='op'>

<option value="+">+</option>

<option value="-">-</option>

<option value="×">×</option>

<option value="÷">÷</option>

<option value="%">%</option>

</select>

数值2:

<input type="text" name="num2"/>

=

<input type="submit" name="act" value="计算">

</form>

</body>

</html>

<?php

error_reporting(E_ALL&~E_NOTICE);

//接收数据

$num1 = $_GET['num1'];

$num2 = $_GET['num2'];

$op = $_GET['op'];

$act = $_GET['act'];

if(is_numeric($num1)&&is_numeric($num2)){

switch($op){

case '+' :

$res = $num1 + $num2;

break;

case '-' :

$res = $num1 - $num2;

break;

case '×' :

$res = $num1 * $num2;

break;

case '÷' :

if($num2 != 0){

$res = $num1 / $num2;

}else{

exit('0不能当除数');

}

break;

case '%' :

$res = $num1 % $num2;

break;

}

}else{

exit('请输入合法数字');

}

echo '<br/>'.'计算结果为:'.$num1.$op.$num2.'='.$res;

?>


写回答

1回答

好帮手慕查理

2019-03-04

您好,可以先判断是否存在get数据,存在再执行。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
<form action="#" method="get">
 数值1:
    <input type="text" name="num1"/>
    <select name='op'>
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="×">×</option>
        <option value="÷">÷</option>
        <option value="%">%</option>
    </select>
 数值2:
    <input type="text" name="num2"/>
 =
    <input type="submit" name="act" value="计算">
</form>
</body>
</html>
<?php
//接收数据
if($_GET){
 $num1 = $_GET['num1'];
 $num2 = $_GET['num2'];
 $op = $_GET['op'];
 $act = $_GET['act'];
 if(is_numeric($num1)&&is_numeric($num2)){
 switch($op){
 case '+' :
 $res = $num1 + $num2;
 break;
 case '-' :
 $res = $num1 - $num2;
 break;
 case '×' :
 $res = $num1 * $num2;
 break;
 case '÷' :
 if($num2 != 0){
 $res = $num1 / $num2;
 }else{
 exit('0不能当除数');
 }
 break;
 case '%' :
 $res = $num1 % $num2;
 break;
 }
    }else{
 exit('请输入合法数字');
 }
 echo '<br/>'.'计算结果为:'.$num1.$op.$num2.'='.$res;
}
?>

祝学习愉快!

0
hante丨R
h 好的,谢谢老师
h019-03-04
共1条回复

0 学习 · 4928 问题

查看课程