使用jsp+JavaBean简单实现计算器

1、实现javabean

package cn.csdn.web.domain; import java.math.BigDecimal; public class Calculate { private Double firstNum; private char operator; private Double secondNum; private Double result; public Calculate() { super(); } public Double getFirstNum() { return firstNum; } public void setFirstNum(Double firstNum) { this.firstNum = firstNum; } public char getOperator() { return operator; } public void setOperator(char operator) { this.operator = operator; } public Double getSecondNum() { return secondNum; } public void setSecondNum(Double secondNum) { this.secondNum = secondNum; } public Double getResult() { return result; } public void setResult(Double result) { this.result = result; } public Double calculate() { switch (this.operator) { case '+': this.result = this.firstNum + this.secondNum; break; case '-': this.result = this.firstNum - this.secondNum; break; case '*': this.result = this.firstNum * this.secondNum; break; case '/': if (this.secondNum == 0) { System.out.println("除数不能为零"); } else { this.result = this.firstNum / this.secondNum; BigDecimal bigDecimal = new BigDecimal(this.result); bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP); this.result = bigDecimal.doubleValue(); } break; default: System.out.println("无法判断"); break; } return result; } } 2、实现jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <style> table { border-collapse: collapse; } </style> <title>My JSP 'calculate.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div align="center"> <h1> 计算哭器简单实现 </h1> <hr color="red"> <div> <!-- 创建一个javabean组件 --> <jsp:useBean id="calculate" class="cn.csdn.web.domain.Calculate"></jsp:useBean> <jsp:setProperty property="*" name="calculate" /> <% calculate.calculate(); %> 计算结果是: <jsp:getProperty property="firstNum" name="calculate" /> <jsp:getProperty property="operator" name="calculate" /> <jsp:getProperty property="secondNum" name="calculate" />= <jsp:getProperty property="result" name="calculate" /> </div> <form action="./calculate.jsp" method="post"> <table border="1px"> <caption> 计算器 </caption> <tr> <td colspan="2"></td> </tr> <tr> <td> 第一个参数: </td> <td> <input type="text" name="firstNum" /> </td> </tr> <tr> <td> 运算符 </td> <td> <select name="operator"> <option selected="selected"> + </option> <option> - </option> <option> * </option> <option> / </option> </select> </td> </tr> <tr> <td> 第二个参数: </td> <td> <input type="text" name="secondNum" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="计算" /> </td> </tr> </table> </form> </div> </body> </html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值