首页 > 代码库 > [Java.web]简单计算器

[Java.web]简单计算器

项目的  WebRoot 目录下的 calculator.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
<head>
<title>计算结果</title>
</head>
<body>
    <jsp:useBean id="calculatorBean"
        class="cn.itcast.domain.CalculatorBean"></jsp:useBean>

    <jsp:setProperty property="*" name="calculatorBean" />
    <%
        try {
            calculatorBean.calculate();
        } catch (Exception ex) {
            out.write("计算出错:<font color=‘red‘>" + ex.getMessage()
                    + "</font>");
        }
    %>

    <hr />
    <label>计算的结果是:<span id="x1"> <jsp:getProperty
                property="arg1" name="calculatorBean" /> </span> <span id="x2"><jsp:getProperty
                property="operator" name="calculatorBean" /></span> <span id="x3">
            <jsp:getProperty property="arg2" name="calculatorBean" /> </span> = <span
        id="x4"> <jsp:getProperty property="result"
                name="calculatorBean" /> </span> </label>
    <hr />

    <form method="post" action="#">
        <table border="1">
            <tr>
                <td colspan="2">简单计算器</td>
            </tr>
            <tr>
                <td>第一个参数</td>
                <td><input type="text" name="arg1" />
                </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>第二个参数</td>
                <td><input type="text" name="arg2" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="计算" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
项目的 src 目录下的 包下面的 CalculatorBean.java
package cn.itcast.domain;

import java.math.BigDecimal;

// 封装计算方法 Bean
public class CalculatorBean {
    private String arg1 = "0";

    private String arg2 = "0";

    private char operator = ‘+‘;

    private String result;

    public String getArg1() {
        return arg1;
    }

    public void setArg1(String arg1) {
        this.arg1 = arg1;
    }

    public String getArg2() {
        return arg2;
    }

    public void setArg2(String arg2) {
        this.arg2 = arg2;
    }

    public char getOperator() {
        return operator;
    }

    public void setOperator(char operator) {
        this.operator = operator;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public void calculate() {
        BigDecimal firstBigDecimal = new BigDecimal(arg1);
        BigDecimal sencondBigDecimal = new BigDecimal(arg2);

        switch (this.operator) {
        case ‘+‘:
            result = firstBigDecimal.add(sencondBigDecimal).toString();
            break;
        case ‘-‘:
            result = firstBigDecimal.subtract(sencondBigDecimal).toString();
            break;
        case ‘*‘:
            result = firstBigDecimal.multiply(sencondBigDecimal).toString();
            break;
        case ‘/‘:
            if (sencondBigDecimal.doubleValue() == 0) {
                //result = "除零错误!";
                //break;
                throw new RuntimeException("除零错误!");
            }
            result = firstBigDecimal.divide(sencondBigDecimal, 20,
                    BigDecimal.ROUND_HALF_UP).toString();
            break;
        default:
            result = "未设定的操作!" + String.valueOf(this.operator);
            break;
        }
    }
}

 附上效果图:

技术分享

 

[Java.web]简单计算器