JSP+JavaBean实现简单计算器

实验5 JavaBean创建和使用
一、实验目的
1、掌握JavaBean创建和使用方法;
2、领会JavaBean简化页面设计的优势等。
二、实验内容
1、设计诸如以下页面的简单计算器
要求:完成加减乘除
(1) 加法、减法如下:
这里写图片描述
(2) 乘法如下:
这里写图片描述
(3)当除数为零时提示报错:
这里写图片描述

这里写图片描述

2.将加减乘除部分写在JavaBean中,在JSP页面中调用显示计算结果。

三、实验方法
1、按照JavaBean书写规则,实现加减乘除;
2、主页面JSP中,设置供客户端输入的文本框。并显示最终计算结果。

四、实验学时:2学时(第9周)
五、实验代码:

calculater.jsp(页面显示)

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <jsp:useBean id="calculater" scope="request" class="exp5.calculater" />

    <jsp:setProperty name="calculater" property="*" />
    <form action="calculater.jsp" method="get">
        <hr align="left" style="width: 400px;">
        计算结果是:
        <span> 
            <%
            if(request.getParameter("operator") != null){
                String operator = request.getParameter("operator");
                int oper = Integer.parseInt(operator);
                if(oper == 4 && calculater.getNum2() == 0){
                    out.print("出错,除数不能为零!");
                }else{
                %>
                <%=calculater.getNum1()%> 
                <%

                    if(oper == 1) out.print("+");
                    if(oper == 2) out.print("-");
                    if(oper == 3) out.print("*");
                    if(oper == 4) out.print("/");

                %>
                <%=calculater.getNum2()%> 
                = 
                <%=calculater.getResult()%> 
            <%
                }
            } 
            %> 

        </span>
        <!-- 表达式显示 -->
        <hr align="left" style="width: 400px;">
        <div align="left" style="width: 400px;">
            <p align="center">简单计算器</p>
            第一个参数:<input type="text" name="num1" /><br>
            <div style="padding-left: 100px;">
                <select name="operator">
                    <option value="1">+</option>
                    <option value="2">-</option>
                    <option value="3">*</option>
                    <option value="4">/</option>
                </select>
            </div>
            第二个参数:<input type="text" name="num2" /><br> <input type="submit"
                value="计算" style="margin-left: 100px;" />
        </div>
    </form>
</body>
</html>

calculater.java(JavaBean逻辑计算处理)

package exp5;

/**
 * @author yilong 创建时间:2017-4-19 下午3:06:34
 * 
 */
public class calculater {
    float num1; // 参数1
    int operator; // 运算符
    float num2; // 参数2
    float result; // 运算结果

    public calculater() {
        super();
    }

    public float getNum1() {
        return num1;
    }

    public void setNum1(float num1) {
        this.num1 = num1;
    }

    public int getOperator() {
        return operator;
    }

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

    public float getNum2() {
        return num2;
    }

    public void setNum2(float num2) {
        this.num2 = num2;
    }

    public float getResult() { // 计算式的运算结果
        float result1 = 0;
        try {
            switch (operator) {
            case 1:
                result1 = num1 + num2;
                break;
            case 2:
                result1 = num1 - num2;
                break;
            case 3:
                result1 = num1 * num2;
                break;
            case 4:
                result1 = num1 / num2;
                break;
            default:
                break;
            }
        } catch (Exception e) {
            e.getMessage();
        }

        return result1;
    }

}

效果展示

这里写图片描述

这里写图片描述

  • 4
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的Java Web计算器实现过程: 1. 首先创建一个JSP页面,用于显示计算器界面。在该页面中,需要有一些按钮,如数字、运算符、清除、等于等。 2. 创建一个JavaBean类,用于处理计算器的逻辑。在该类中,需要定义一些成员变量,如操作数1、操作数2、运算符等,以及一些方法,如加、减、乘、除等。 3. 在JSP页面中,使用`<jsp:useBean>`指令引用JavaBean类,并通过`<jsp:setProperty>`指令设置JavaBean类的属性。 4. 在JSP页面中,使用`<% %>`标签引用JavaBean类中的方法,用于处理用户的操作。例如,当用户点击加号按钮时,调用JavaBean类中的加法方法。 5. 最后,使用`<%= %>`标签输出计算结果。 下面是一个简单的Java Web计算器的代码实现: Calculator.jsp: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.example.bean.CalculatorBean" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Calculator</title> </head> <body> <h1>Calculator</h1> <form method="post"> <table> <tr> <td colspan="4"><input type="text" name="result" value="${calculatorBean.getResult()}" /></td> </tr> <tr> <td><input type="button" value="7" onclick="document.forms[0].result.value += '7'" /></td> <td><input type="button" value="8" onclick="document.forms[0].result.value += '8'" /></td> <td><input type="button" value="9" onclick="document.forms[0].result.value += '9'" /></td> <td><input type="button" value="+" onclick="document.forms[0].operator.value = '+'" /></td> </tr> <tr> <td><input type="button" value="4" onclick="document.forms[0].result.value += '4'" /></td> <td><input type="button" value="5" onclick="document.forms[0].result.value += '5'" /></td> <td><input type="button" value="6" onclick="document.forms[0].result.value += '6'" /></td> <td><input type="button" value="-" onclick="document.forms[0].operator.value = '-'" /></td> </tr> <tr> <td><input type="button" value="1" onclick="document.forms[0].result.value += '1'" /></td> <td><input type="button" value="2" onclick="document.forms[0].result.value += '2'" /></td> <td><input type="button" value="3" onclick="document.forms[0].result.value += '3'" /></td> <td><input type="button" value="*" onclick="document.forms[0].operator.value = '*'" /></td> </tr> <tr> <td><input type="button" value="0" onclick="document.forms[0].result.value += '0'" /></td> <td><input type="button" value="C" onclick="document.forms[0].result.value = ''" /></td> <td><input type="submit" value="=" /></td> <td><input type="button" value="/" onclick="document.forms[0].operator.value = '/'" /></td> </tr> <tr> <td colspan="4"><input type="hidden" name="operator" /></td> </tr> </table> </form> </body> </html> ``` CalculatorBean.java: ```java package com.example.bean; public class CalculatorBean { private double operand1; private double operand2; private String operator; private double result; public CalculatorBean() { } public double getOperand1() { return operand1; } public void setOperand1(double operand1) { this.operand1 = operand1; } public double getOperand2() { return operand2; } public void setOperand2(double operand2) { this.operand2 = operand2; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } public void add() { result = operand1 + operand2; } public void subtract() { result = operand1 - operand2; } public void multiply() { result = operand1 * operand2; } public void divide() { result = operand1 / operand2; } } ``` 在以上代码中,我们通过`<jsp:useBean>`指令引用了CalculatorBean类,并使用`<jsp:setProperty>`指令设置了CalculatorBean类的属性。在表单提交时,我们通过JavaScript获取到用户的操作数和运算符,然后调用相应的JavaBean方法处理计算逻辑,最后通过`<%= %>`标签输出计算结果。 希望这篇文章能够帮助你实现一个简单的Java Web计算器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值