使用jsp+JavaBean简单实现计算器

1、实现javabean

package cn.csdn.web.domain; import java.math.BigDecimal; public class Calculate { private Double firstNum; private char operator; private Double secondNum; private Double result; public Calculate() { super(); } public Double getFirstNum() { return firstNum; } public void setFirstNum(Double firstNum) { this.firstNum = firstNum; } public char getOperator() { return operator; } public void setOperator(char operator) { this.operator = operator; } public Double getSecondNum() { return secondNum; } public void setSecondNum(Double secondNum) { this.secondNum = secondNum; } public Double getResult() { return result; } public void setResult(Double result) { this.result = result; } public Double calculate() { switch (this.operator) { case '+': this.result = this.firstNum + this.secondNum; break; case '-': this.result = this.firstNum - this.secondNum; break; case '*': this.result = this.firstNum * this.secondNum; break; case '/': if (this.secondNum == 0) { System.out.println("除数不能为零"); } else { this.result = this.firstNum / this.secondNum; BigDecimal bigDecimal = new BigDecimal(this.result); bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP); this.result = bigDecimal.doubleValue(); } break; default: System.out.println("无法判断"); break; } return result; } } 2、实现jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> My JSP 'calculate.jsp' starting page

计算哭器简单实现


<% calculate.calculate(); %> 计算结果是: =
计算器
第一个参数:
运算符
第二个参数:



你可能感兴趣的:(使用jsp+JavaBean简单实现计算器)