---
title: "JavaBean创建和使用：计算器"
author: "leaf-stop"
date: "2016-06-01T14:01:47Z"
tags: []
categories: ["编程语言"]
slug: "javabean"
draft: false
lastmod: "2016-09-09T11:26:19Z"
id: 2302381
url: "https://leaf-stop.iteye.com/blog/2302381"
---

JavaBean创建和使用——计算器

1、掌握JavaBean创建和使用方法；

2、领会JavaBean简化页面设计的优势等。

设计简单计算器

要求：完成加减乘除

    当除数为零时报错

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

Calculator.java

 

```
package com.jk.yt;

import java.math.BigDecimal;

public class Calculator {

	private String onenum = "0";
	private String twonum = "0";
	private char fuhao = '+';
	private String result;

	public String getOnenum() {
		return onenum;
	}

	public void setOnenum(String onenum) {
		this.onenum = onenum;
	}

	public String getTwonum() {
		return twonum;
	}

	public void setTwonum(String twonum) {
		this.twonum = twonum;
	}

	public char getFuhao() {
		return fuhao;
	}

	public void setFuhao(char fuhao) {
		this.fuhao = fuhao;
	}

	public String getResult() {
		return result;
	}

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

	public void calculate(){
		BigDecimal a=new BigDecimal(this.onenum);
		BigDecimal b=new BigDecimal(this.twonum);
		switch (this.fuhao) {
		case '+':
			this.result=a.add(b).toString();
			break;
			
		case '-':
			this.result=a.subtract(b).toString();
			break;

		case '*':
			this.result=a.multiply(b).toString();
			break;
			
		case '/':
			if(b.doubleValue()==0){
				throw new RuntimeException("<h1>sorry，出错了！！除数不能为零</h1>");
			}
			this.result=a.divide(b,10,BigDecimal.ROUND_HALF_DOWN).toString();
			break;
			
		default:
			break;
		}
	}
}
```

 index.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>计算器</title>
<style type="text/css">
#top {
	text-align: center;
}

table {
	width:330px;
	height:220px;
	text-align: center;	
	margin: 0 auto;
	background: url(4.jpg);
}
</style>

</head>
<body>
	<jsp:useBean id="calculate" class="com.jk.yt.Calculator"></jsp:useBean>
	<jsp:setProperty property="*" name="calculate" />
	<%
		try {
			calculate.calculate();
	%>
	<div id="top">
		<h1>
			计算结果：
			<jsp:getProperty property="onenum" name="calculate" />
			<jsp:getProperty property="fuhao" name="calculate" />
			<jsp:getProperty property="twonum" name="calculate" />
			=<jsp:getProperty property="result" name="calculate" />
		</h1>
		<hr>
	</div>

	<form action="index.jsp" method="post" name="form">
		<table>
			<tr>
				<td colspan="2"><h1>简单的计算器</h1></td>
			</tr>
			<tr>
				<td>第一个参数</td>
				<td><input type="text" name="onenum" /></td>
			</tr>
			<tr>
				<td colspan="2"><select name="fuhao" id="jisuan">
						<option value="+">+</option>
						<option value="-">-</option>
						<option value="*">*</option>
						<option value="/">/</option>
				</select></td>
			</tr>
			<tr>
				<td>第二个参数</td>
				<td><input type="text" name="twonum" /></td>
			</tr>
			<tr>
				<td colspan="2">
					<button type="submit">计算</button>
				</td>
			</tr>
		</table>
	</form>
	<%
		} catch (Exception e) {
			out.write(e.getMessage());
		}
	%>

</body>
</html>
```

 最终结果如图所示：

![](http://dl2.iteye.com/upload/attachment/0117/3585/9e45a46c-da31-31e1-968f-e111c5d23350.png)

加法：  
![](http://dl2.iteye.com/upload/attachment/0117/3587/d7209180-51cf-3f24-82be-f62ec1bff47e.png)  
 乘法：  
![](http://dl2.iteye.com/upload/attachment/0117/3589/7d73dd76-f8e1-37e1-9bbd-dd55efa581a6.png)  
 除法，除数为零时报错  
![](http://dl2.iteye.com/upload/attachment/0117/3591/5c206eb4-4885-3fdb-b385-6b8b0f13fc70.png)  
![](http://dl2.iteye.com/upload/attachment/0117/3593/3925b84d-2e95-3a3d-a7af-bf44c3db5058.png)

代码上传至附件：Jspwork501.zip