javabean实现小计算器

来源:互联网 发布:饮食男女知乎 编辑:程序博客网 时间:2024/06/06 08:55
 

1、实现javabean

view plaincopy to clipboardprint?
  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页面

view plaincopy to clipboardprint?
  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>  

来自任海丽