第78讲 面向对象编程综合练习
来源:互联网 发布:淘宝网安踏春秋长裤 编辑:程序博客网 时间:2024/06/07 11:04
<!doctype html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>计算器</title> </head> <body> <form action="test078Result.php" method="post"> <table width="300px" border="0"> <td>第一个数</td> <td><input type="text" name="num1"/></td> <tr/> <td>第二个数</td> <td><input type="text" name="num2"/></td> <tr/> <td>运算符</td> <td> <select name="oper"/> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> </td> <tr> <td colspan="2"><input type="submit" value="计算结果"/></td> </tr> </table> </form> </body></html>
<?php $num1=$_REQUEST['num1']; //2、接收num2 $num2=$_REQUEST['num2']; //3、接收运算符 $oper=$_REQUEST['oper']; class OperService{ public $numOne; public $numTwo; public $operShow; public function setData($num1,$num2,$oper){ $this->numOne = $num1; $this->numTwo = $num2; $this->operShow = $oper; } public function getLastShow(){ $lastShow = 0; switch($this->operShow){ case "+": $lastShow = $this->numOne + $this->numTwo; break; case "-": $lastShow = $this->numOne - $this->numTwo; break; case "*": $lastShow = $this->numOne * $this->numTwo; break; case "/": $lastShow = $this->numOne / $this->numTwo; break; } return $lastShow; } } $operService = new OperService(); $operService->setData($num1,$num2,$oper); print $operService->getLastShow();?>
结果:按要求输出。。。。
新版:
在html页面增加数据校验:
<!doctype html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>计算器</title> //增加js数据检验--有这个值不对的话浏览器就有弹窗了 <script type="text/javascript"> function check(){ var num1 = document.getElementById("num1").value; var num2 = document.getElementById("num2").value; //window.alert(num1+" "+num2); if(isNaN(num1)||isNaN(num1)){ window.alert("请确认输入的值是否整数"); return false; } } </script> </head> <body> <form action="test078Result.php" method="post" onsubmit="return check()"> <table width="300px" border="0"> <td>第一个数</td> <td><input type="text" id="num1" name="num1"/></td> <tr/> <td>第二个数</td> <td><input type="text" id="num2" name="num2"/></td> <tr/> <td>运算符</td> <td> <select name="oper"/> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> </td> <tr> <td colspan="2"><input type="submit" value="计算结果"/></td> </tr> </table> </form> </body></html>
结果处理界面增加数据接收判断:
<?php if(isset($_REQUEST['num1'])){ $num1=$_REQUEST['num1']; } if(isset($_REQUEST['num2'])){ //2、接收num2 $num2=$_REQUEST['num2']; } if(isset($_REQUEST['oper'])){ //3、接收运算符 $oper=$_REQUEST['oper']; } class OperService{ public $numOne; public $numTwo; public $operShow; public function setData($num1,$num2,$oper){ $this->numOne = $num1; $this->numTwo = $num2; $this->operShow = $oper; } public function getLastShow(){ $lastShow = 0; switch($this->operShow){ case "+": $lastShow = $this->numOne + $this->numTwo; break; case "-": $lastShow = $this->numOne - $this->numTwo; break; case "*": $lastShow = $this->numOne * $this->numTwo; break; case "/": $lastShow = $this->numOne / $this->numTwo; break; } return $lastShow; } } $operService = new OperService(); $operService->setData($num1,$num2,$oper); print $operService->getLastShow();?>
阅读全文
0 0
- 第78讲 面向对象编程综合练习
- 《Python核心编程》第13章 面向对象编程 练习
- 答答租车系统(面向对象综合练习)
- 答答租车系统(面向对象综合练习)
- 答答租车系统(面向对象综合练习)
- 答答租车系统(面向对象综合练习)
- C#面向对象编程练习
- 面向对象的编程练习
- 第06讲 面向对象示例
- 【Java】第1讲:面向对象
- Python语法第9讲:面向对象
- 第67+68讲 php面向对象编程 this 以及构造方法
- 面向对象编程 第二讲091107
- 8、黑马程序员-面向对象的综合练习
- java_答答租车系统(面向对象综合练习)_sdut
- JAVA面向对象练习05第一种
- 集合List与面向对象编程综合应用
- C语言初步-第39讲:问题求解——求素数(练习综合)
- 解决NPM使用下载慢或者无法下载的问题error
- Tablayout的使用
- socket 并发服务器 代码案例 (多进程模式)
- spring boot学习(六)---表单验证
- 进程通信之无名pipe
- 第78讲 面向对象编程综合练习
- 基于nginx的中间件架构(1)环境准备
- __bridge 显式转换 id和void *
- bzoj 5047 空间传送装置 最短路
- 在Mac中配置tomcat服务器教程(超详细配图)以及其配置简单原理
- Codeforces 873B Balanced Substring(利用前缀和找和为0的子串)
- lwip ping时间长解决办法
- 深度学习lstm防止过拟合之dropout
- Android指纹