JavaWeb_jsp_简单计算器(jsp+javaBean模式)

BigDecimal演示

import java.math.BigDecimal;
public class BigDecimalDemo {
    public static void main(String[] args) {
        method_3();
    }
    private static void method_3() {
        BigDecimal a=new BigDecimal("0.1");
        BigDecimal b=new BigDecimal("0.006");
        BigDecimal c1=a.add(b);
        BigDecimal c2=a.subtract(b);
        BigDecimal c3=a.multiply(b);
        //除法必须指定小数位数(scale),否则会报ArithmeticException
        System.out.println(c1.toString());
        //0.106
        System.out.println(c2.toString());
        //0.094
        System.out.println(c3.toString());
        //0.0006
        BigDecimal c4=a.divide(b,10,BigDecimal.ROUND_CEILING);
        System.out.println(c4.toString());
        //16.6666666667
        BigDecimal c5=a.divide(b,10,BigDecimal.ROUND_FLOOR);
        System.out.println(c5.toString());
        //16.6666666666
        BigDecimal c6=a.divide(b,10,BigDecimal.ROUND_DOWN);
        System.out.println(c6.toString());
        //16.6666666666
        BigDecimal c7=a.divide(b,10,BigDecimal.ROUND_UP);
        System.out.println(c7.toString());
        //16.6666666667
        BigDecimal c8=a.divide(b,10,BigDecimal.ROUND_HALF_UP);
        System.out.println(c8.toString());
        //16.6666666667
        BigDecimal c9=a.divide(b,10,BigDecimal.ROUND_HALF_DOWN);
        System.out.println(c9.toString());
        //16.6666666667
        /*
static int    ROUND_CEILING 
                  接近正无穷大的舍入模式。
static int    ROUND_FLOOR 
                接近负无穷大的舍入模式。
static int    ROUND_UP 
        舍入远离零的舍入模式。
static int    ROUND_DOWN 
        接近零的舍入模式。
static int    ROUND_HALF_UP 
        向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。
static int    ROUND_HALF_DOWN 
        向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为上舍入的舍入模式。
static int    ROUND_HALF_EVEN 
        向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。
static int    ROUND_UNNECESSARY 
        断言请求的操作具有精确的结果,因此不需要舍入。
        */

    }
    private static void method_2() {
        BigDecimal a=new BigDecimal(0.1);
        BigDecimal b=new BigDecimal(0.006);
        BigDecimal c=a.add(b);
        System.out.println(c.toString());//直接传double照样出错
        //0.10600000000000000567601521339611281291581690311431884765625
    }
    private static void method_1() {
        double a=0.1;
        double b=0.006;
        System.out.println(a+b);
        //0.10600000000000001
    }
}


calculator.jsp负责显示数据和处理请求

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>计算器</title>
    <style type="text/css" >
    #calculate{
        color: red;
    }
    </style>
  </head>
  <body style="text-align: center;">
  第一种开发模式:JSP+JAVABEAN
  jsp:既负责显示数据,又负责处理请求!
  javaBean:专门封装数据
  <jsp:useBean id="calc" class="cn.itcast.domain.CalculatorBean" scope="page" />
  <jsp:setProperty name="calc" property="*"/>
  <div id="calculate">
  <%
          try{
              calc.calculate();
          }catch(Exception e){
              String str=e.getMessage();
              out.write(str);
          }
   %>
   </div>
   <hr/>
   计算结果是:${calc.firstNumber }${calc.operator }${calc.secondNumber }=${calc.result }
   <hr/>
  <form action="${pageContext.request.contextPath }/calculator.jsp" method="post">
    <table width="40%" border="1">
        <tr >
            <td colspan="2" align="center">简单的计算器</td>
        </tr>
        <tr>
            <td>第1个参数:</td>
            <td>
                    <input type="text" name="firstNumber" /> 
            </td>
        </tr>
        <tr>
            <td>操作符</td>
            <td>
                <select name="operator">
                    <option value="+">+</option>
                    <option value="-">-</option>
                    <option value="*">*</option>
                    <option value="/">/</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>第2个参数:</td>
            <td>
                    <input type="text" name="secondNumber" /> 
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                    <input type="submit" value="计算"/> 
            </td>
        </tr>
    </table>
    </form>
  </body>
</html>


CalculatorBean封装数据

package cn.itcast.domain;
import java.math.BigDecimal;
public class CalculatorBean {
    private String firstNumber="0";
    private char operator='+';
    private String secondNumber="0";
    private String result;
    public void calculate(){
        BigDecimal a=new BigDecimal(this.firstNumber);
        BigDecimal b=new BigDecimal(this.secondNumber);
        switch (this.operator) {
        case '+':{
            this.result=a.add(b).toString();
            break;
        }
        case '-':{
            this.result=a.subtract(b).toString();
            break;
        }
        case '*':{
            this.result=a.multiply(b).toString();
            break;
        }
        case '/':{
            if (b.doubleValue()==0) {
                throw new RuntimeException("被除数不能为0");
            }
            this.result=a.divide(b,5,BigDecimal.ROUND_CEILING).toString();
            break;
        }
        default:
            throw new RuntimeException("运算符仅支持+-*/");
        }
    }
    public String getFirstNumber() {
        return firstNumber;
    }
    public void setFirstNumber(String firstNumber) {
        this.firstNumber = firstNumber;
    }
    public char getOperator() {
        return operator;
    }
    public void setOperator(char operator) {
        this.operator = operator;
    }
    public String getSecondNumber() {
        return secondNumber;
    }
    public void setSecondNumber(String secondNumber) {
        this.secondNumber = secondNumber;
    }
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }
}


你可能感兴趣的:(java,jsp,javaweb)