• javaWeb 使用 jsp 和 javaBean 实现计算器功能


    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>jsp 和 javaBean 实现计算器功能</title>
    </head>
    <body>
    
        <jsp:useBean id="calculator" class="de.bvb.domain.Calculator" />
        <jsp:setProperty property="*" name="calculator" />
        <%
            try {
                calculator.calculate();
            } catch (Exception e) {
                out.write(e.getMessage());
            }
        %>
    
        <form action="/web/calculator.jsp" method="post">
            <table width="50%" border="1">
                <tr>
                    <td colspan="2">简单的计算器</td>
                </tr>
                <tr>
                    <td>第一个参数</td>
                    <td><input type="text" name="firstNum" value="11"></td>
                </tr>
    
                <tr>
                    <td>运算符</td>
                    <td><select name="operator">
                            <option value="+">+</option>
                            <option value="-">-</option>
                            <option value="*">*</option>
                            <option value="/" selected="selected">/</option>
                    </select></td>
                </tr>
    
                <tr>
                    <td>第二个参数</td>
                    <td><input type="text" name="secondNum" value="0" ></td>
                </tr>
    
                <tr>
                    <td colspan="2"><input type="submit" value="计算"></td>
                </tr>
    
                <tr>
                    <td>结果</td>
                    <td>
                        <jsp:getProperty property="firstNum" name="calculator" /> 
                        <jsp:getProperty property="operator" name="calculator" /> 
                        <jsp:getProperty property="secondNum" name="calculator" />
                         = 
                         <jsp:getProperty property="result" name="calculator" />
                    </td>
                </tr>
    
            </table>
    
        </form>
    </body>
    </html>
    package de.bvb.domain;
    
    import java.math.BigDecimal;
    
    public class Calculator {
        private String firstNum = "0";
        private char operator = '+';
        private String secondNum = "0";
        private String result;
    
        public void calculate() {
            BigDecimal first = new BigDecimal(firstNum);
            BigDecimal seconde = new BigDecimal(secondNum);
            switch (operator) {
            case '+':
                result = first.add(seconde).toString();
                break;
            case '-':
                result = first.subtract(seconde).toString();
                break;
            case '*':
                result = first.multiply(seconde).toString();
                break;
            case '/':
                if (Double.parseDouble(secondNum) == 0) {
                    throw new RuntimeException("除数不能为0");
                }
                result = first.divide(seconde, 20, BigDecimal.ROUND_HALF_UP)
                        .toString();
                break;
            default:
                throw new RuntimeException("数据或操作符异常");
            }
        }
    
        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;
        }
    
    }
  • 相关阅读:
    tar命令详解
    保存GNOME桌面环境中声卡的音量设置
    SyntaxError: NonASCII character ‘\xe5′ in file
    Python小练习采集天气预报
    Debian编译内核教程
    Debian软件包依赖关系图
    mysql unauthenticated user原因分析以及解决方法
    Struts2 配置
    Map基本操作
    Iterator迭代器
  • 原文地址:https://www.cnblogs.com/Westfalen/p/5968749.html
Copyright © 2020-2023  润新知