java(17)--用jsp+javabean实现简单的计算机

javabean

import java.math.BigDecimal;

//封装计算器数据的bean
public class CalculatorBean {

    private String firstNum = "0";
    private char operator = '+';   //int  char
    private String secondNum = "0";
    private String result;


    public String getFirstNum() {
        return firstNum;
    }
    public void setFirstNum(String firstNum) {
        this.firstNum = firstNum;
    }

    public char getOperator() {
        return operator;
    }
    public void setOperator(char operator) {
        this.operator = operator;
    }
    public String getSecondNum() {
        return secondNum;
    }
    public void setSecondNum(String secondNum) {
        this.secondNum = secondNum;
    }
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }

    public void calculate(){
        BigDecimal first = new BigDecimal(this.firstNum);
        BigDecimal second = new BigDecimal(this.secondNum);

        switch (this.operator) {
            case '+':{
                this.result = first.add(second).toString();
                break;
            }
            case '-':{
                this.result = first.subtract(second).toString();
                break;
            }
            case '*':{
                this.result = first.multiply(second).toString();
                break;
            }
            case '/':{
                if(second.doubleValue()==0){
                    throw new RuntimeException("被除数不能为0");
                }
                this.result = first.divide(second,20,BigDecimal.ROUND_HALF_UP).toString();
                break;
            }
            default:
                throw new RuntimeException("运算符只能是:+-*/   ");
        }   
    }
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>计算器</title>
  </head>

  <body style="text-align: center;">

        <jsp:useBean id="calculatorBean" class="zgq.domain.CalculatorBean"/>
        <jsp:setProperty name="calculatorBean" property="*"/>
        <% 
            try{
                calculatorBean.calculate();
            }catch(Exception e){
                out.write(e.getMessage());
            }
        %>

        <br/>------------------------------------------------------------------------------------------------------------------------<br/>
        计算结果是:
        <jsp:getProperty name="calculatorBean" property="firstNum"/>
        <jsp:getProperty name="calculatorBean" property="operator"/>
        <jsp:getProperty name="calculatorBean" property="secondNum"/>
        =
        <jsp:getProperty name="calculatorBean" property="result"/>

        <br/>------------------------------------------------------------------------------------------------------------------------<br/>

        <br/>

        <form action="/day09/calculator.jsp" method="post">
        <table width="40%" border="1">
            <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 value="+">+</option>
                        <option value="-">-</option>
                        <option value="*">*</option>
                        <option value="/">/</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>

  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值