javabean实现小计算器

 

1、实现javabean

  1. package cn.csdn.web.domain;  
  2.   
  3. import java.math.BigDecimal;  
  4.   
  5. public class Calculate {  
  6.     private Double firstNum;  
  7.     private char operator;  
  8.     private Double secondNum;  
  9.     private Double result;  
  10.   
  11.     public Calculate() {  
  12.         super();  
  13.     }  
  14.   
  15.     public Double getFirstNum() {  
  16.         return firstNum;  
  17.     }  
  18.   
  19.     public void setFirstNum(Double firstNum) {  
  20.         this.firstNum = firstNum;  
  21.     }  
  22.   
  23.     public char getOperator() {  
  24.         return operator;  
  25.     }  
  26.   
  27.     public void setOperator(char operator) {  
  28.         this.operator = operator;  
  29.     }  
  30.   
  31.     public Double getSecondNum() {  
  32.         return secondNum;  
  33.     }  
  34.   
  35.     public void setSecondNum(Double secondNum) {  
  36.         this.secondNum = secondNum;  
  37.     }  
  38.   
  39.     public Double getResult() {  
  40.         return result;  
  41.     }  
  42.   
  43.     public void setResult(Double result) {  
  44.         this.result = result;  
  45.     }  
  46.   
  47.     public Double calculate() {  
  48.         switch (this.operator) {  
  49.         case '+':  
  50.             this.result = this.firstNum + this.secondNum;  
  51.             break;  
  52.         case '-':  
  53.             this.result = this.firstNum - this.secondNum;  
  54.             break;  
  55.         case '*':  
  56.             this.result = this.firstNum * this.secondNum;  
  57.             break;  
  58.         case '/':  
  59.             if (this.secondNum == 0) {  
  60.                 System.out.println("除数不能为零");  
  61.             } else {  
  62.                 this.result = this.firstNum / this.secondNum;  
  63.                 BigDecimal bigDecimal = new BigDecimal(this.result);  
  64.                 bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);  
  65.                 this.result = bigDecimal.doubleValue();  
  66.             }  
  67.             break;  
  68.         default:  
  69.             System.out.println("无法判断");  
  70.             break;  
  71.         }  
  72.         return result;  
  73.     }  
  74. }  
2、实现jsp页面

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"  
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.     <head>  
  12.         <base href="<%=basePath%>">  
  13.         <style>  
  14. table {  
  15.     border-collapse: collapse;  
  16. }  
  17. </style>  
  18.         <title>My JSP 'calculate.jsp' starting page</title>  
  19.   
  20.         <meta http-equiv="pragma" content="no-cache">  
  21.         <meta http-equiv="cache-control" content="no-cache">  
  22.         <meta http-equiv="expires" content="0">  
  23.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  24.         <meta http-equiv="description" content="This is my page">  
  25.         <!-- 
  26.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  27.     -->  
  28.   
  29.     </head>  
  30.   
  31.     <body>  
  32.         <div align="center">  
  33.             <h1>  
  34.                 计算哭器简单实现  
  35.             </h1>  
  36.             <hr color="red">  
  37.             <div>  
  38.                 <!-- 创建一个javabean组件 -->  
  39.                 <jsp:useBean id="calculate" class="cn.csdn.web.domain.Calculate"></jsp:useBean>  
  40.                 <jsp:setProperty property="*" name="calculate" />  
  41.                 <%  
  42.                     calculate.calculate();  
  43.                 %>  
  44.                 计算结果是:  
  45.                 <jsp:getProperty property="firstNum" name="calculate" />  
  46.                 <jsp:getProperty property="operator" name="calculate" />  
  47.                 <jsp:getProperty property="secondNum" name="calculate" />=  
  48.                 <jsp:getProperty property="result" name="calculate" />  
  49.             </div>  
  50.             <form action="./calculate.jsp" method="post">  
  51.                 <table border="1px">  
  52.                     <caption>  
  53.                         计算器  
  54.                     </caption>  
  55.                     <tr>  
  56.                         <td colspan="2"></td>  
  57.                     </tr>  
  58.                     <tr>  
  59.                         <td>  
  60.                             第一个参数:  
  61.                         </td>  
  62.                         <td>  
  63.                             <input type="text" name="firstNum" />  
  64.                         </td>  
  65.                     </tr>  
  66.                     <tr>  
  67.                         <td>  
  68.                             运算符  
  69.                         </td>  
  70.                         <td>  
  71.                             <select name="operator">  
  72.                                 <option selected="selected">  
  73.                                     +  
  74.                                 </option>  
  75.                                 <option>  
  76.                                     -  
  77.                                 </option>  
  78.                                 <option>  
  79.                                     *  
  80.                                 </option>  
  81.                                 <option>  
  82.                                     /  
  83.                                 </option>  
  84.                             </select>  
  85.                         </td>  
  86.                     </tr>  
  87.                     <tr>  
  88.                         <td>  
  89.                             第二个参数:  
  90.                         </td>  
  91.                         <td>  
  92.                             <input type="text" name="secondNum" />  
  93.                         </td>  
  94.                     </tr>  
  95.                     <tr>  
  96.                         <td colspan="2">  
  97.                             <input type="submit" value="计算" />  
  98.                         </td>  
  99.                     </tr>  
  100.                 </table>  
  101.   
  102.             </form>  
  103.         </div>  
  104.     </body>  
  105. </html>  

来自任海丽

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Java Web计算器实现过程: 1. 首先创建一个JSP页面,用于显示计算器界面。在该页面中,需要有一些按钮,如数字、运算符、清除、等于等。 2. 创建一个JavaBean类,用于处理计算器的逻辑。在该类中,需要定义一些成员变量,如操作数1、操作数2、运算符等,以及一些方法,如加、减、乘、除等。 3. 在JSP页面中,使用`<jsp:useBean>`指令引用JavaBean类,并通过`<jsp:setProperty>`指令设置JavaBean类的属性。 4. 在JSP页面中,使用`<% %>`标签引用JavaBean类中的方法,用于处理用户的操作。例如,当用户点击加号按钮时,调用JavaBean类中的加法方法。 5. 最后,使用`<%= %>`标签输出计算结果。 下面是一个简单的Java Web计算器的代码实现: Calculator.jsp: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.example.bean.CalculatorBean" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Calculator</title> </head> <body> <h1>Calculator</h1> <form method="post"> <table> <tr> <td colspan="4"><input type="text" name="result" value="${calculatorBean.getResult()}" /></td> </tr> <tr> <td><input type="button" value="7" onclick="document.forms[0].result.value += '7'" /></td> <td><input type="button" value="8" onclick="document.forms[0].result.value += '8'" /></td> <td><input type="button" value="9" onclick="document.forms[0].result.value += '9'" /></td> <td><input type="button" value="+" onclick="document.forms[0].operator.value = '+'" /></td> </tr> <tr> <td><input type="button" value="4" onclick="document.forms[0].result.value += '4'" /></td> <td><input type="button" value="5" onclick="document.forms[0].result.value += '5'" /></td> <td><input type="button" value="6" onclick="document.forms[0].result.value += '6'" /></td> <td><input type="button" value="-" onclick="document.forms[0].operator.value = '-'" /></td> </tr> <tr> <td><input type="button" value="1" onclick="document.forms[0].result.value += '1'" /></td> <td><input type="button" value="2" onclick="document.forms[0].result.value += '2'" /></td> <td><input type="button" value="3" onclick="document.forms[0].result.value += '3'" /></td> <td><input type="button" value="*" onclick="document.forms[0].operator.value = '*'" /></td> </tr> <tr> <td><input type="button" value="0" onclick="document.forms[0].result.value += '0'" /></td> <td><input type="button" value="C" onclick="document.forms[0].result.value = ''" /></td> <td><input type="submit" value="=" /></td> <td><input type="button" value="/" onclick="document.forms[0].operator.value = '/'" /></td> </tr> <tr> <td colspan="4"><input type="hidden" name="operator" /></td> </tr> </table> </form> </body> </html> ``` CalculatorBean.java: ```java package com.example.bean; public class CalculatorBean { private double operand1; private double operand2; private String operator; private double result; public CalculatorBean() { } public double getOperand1() { return operand1; } public void setOperand1(double operand1) { this.operand1 = operand1; } public double getOperand2() { return operand2; } public void setOperand2(double operand2) { this.operand2 = operand2; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } public void add() { result = operand1 + operand2; } public void subtract() { result = operand1 - operand2; } public void multiply() { result = operand1 * operand2; } public void divide() { result = operand1 / operand2; } } ``` 在以上代码中,我们通过`<jsp:useBean>`指令引用了CalculatorBean类,并使用`<jsp:setProperty>`指令设置了CalculatorBean类的属性。在表单提交时,我们通过JavaScript获取到用户的操作数和运算符,然后调用相应的JavaBean方法处理计算逻辑,最后通过`<%= %>`标签输出计算结果。 希望这篇文章能够帮助你实现一个简单的Java Web计算器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值