JSP学习笔记---应用做个简易计算器JSP+javabean

注意:

1.用到了标签,注意javabean中参数变量务必要与表单提交数据名称相同。


Main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



  
    My JSP 'Main.jsp' starting page
  
  
  
  
  
  
<%
     calculate.calculate();
     out.write(calculate.getFirstNum()+calculate.getOperator()+calculate.getSecondNum()+"="+calculate.getResult());
 %>

 
输入第一个数: 选择操作: 输入第二个数:
Calculate.java

package com.wanhao;

import java.math.BigDecimal;

public class Calculate {
    String firstNum="0";
    char operator='+';
    String secondNum="0";
    String result="0";
	public char getOperator() {
		return operator;
	}
	public void setOperator(char operator) {
		this.operator = operator;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String getFirstNum() {
		return firstNum;
	}
	public void setFirstNum(String firstNum) {
		this.firstNum = firstNum;
	}
	public String getSecondNum() {
		return secondNum;
	}
	public void setSecondNum(String secondNum) {
		this.secondNum = secondNum;
	}
	public void calculate()
	{
		BigDecimal  number1=new BigDecimal(firstNum);
		BigDecimal number2=new BigDecimal(secondNum);
         switch(operator)
         {
         case '+':  result=number1.add(number2).toString();break;
         case '-':   result=number1.subtract(number2).toString();break;
         case '*':   result=number1.multiply(number2).toString();break;
         case '/':   result=number1.divide(number2, 10, BigDecimal.ROUND_UP).toString();break;
         default:  result="0";
         }
		
	}
    
}





你可能感兴趣的:(java,javaweb,JSP)