批改状态:合格
老师批语:
echo <<< heredoc<table id="" border="1" ><tr><td width="50px;" bgColor="red">1</td><td width="50px;" bgColor="#E9967A">2</td><td width="50px;" bgColor="#FF8C00">3</td></tr><tr><td width="50px;" bgColor="pink">4</td><td width="50px;" bgColor="#6495ED">5</td><td width="50px;" bgColor="#008B8B">6</td></tr><tr><td width="50px;" bgColor="yellow">7</td><td width="50px;" bgColor="skyblue">8</td><td width="50px;" bgColor="#90EE90">9</td></tr></table>heredoc;
//判断用户是否点击了计算按钮if(isset($_POST['sub'])){ if($_POST['option']==='/'&&$_POST['num2']===0||$_POST['opt']==='%'&&$-POST[num2]===0){$warn="<span style='color:red;'>除数不能为0</span>";}}//html<h1>计算器</h1><table><form action="post" name="compute" id="compute"><tr><td><input type="number" name="num1" required/></td><td>//存放计算器操作符<select name="opt"><option value="+">+</option><option value="-">-</option><option value="*">*</option><option value="/">/</option><option value="%">%</option></select></td><td><input type="number" name="num2" required/></td><td><input type="submit" name="sub" value="确认"/></td></tr></form></table>//判断用户输入的运算符和值if(!$warn){switch($_POST['option']){case "+":$sum=(int)$_POST[num1]+(int)$_POST[num2];break;case "-":$sum=(int)$_POST[num1]-(int)$_POST[num2];break;case "*":$sum=(int)$_POST[num1]*(int)$_POST[num2];break;case "/":$sum=(int)$_POST[num1]/(int)$_POST[num2];break;case "%":$sum=(int)$_POST[num1]%(int)$_POST[num2];break;};//存放计算结果$res="计算结果为 {$_POST['num1']}{$-POST['option']} {$_POST[num1]}={$sum}";echo $res;}else{echo $warn;}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号