使用JSP和javabean实现计算器功能

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/SadOnMyOwn/article/details/51435433

       在进行功能实现之前需要注意一个问题,在java中进行数值计算时,由于不同数据类型所占位数不同,可能使得得到的结果存在一定的误差,一个很简单的例子,比如输入如下简单代码片段:

double a=0.1;
     double b=0.005;
     System.out.println(a+b);

得到的结果为:0.10500000000000001

        为了得到精确计算,使用BigDecimal类中的相关方法进行数值计算。该过程需要两个步骤:

1、封装计算器数据的bean

2、在jsp中进行数据的具体操作,并显示给客户机

       封装计算器数据的bean的主要程序代码为:

public class CalculatorBean {

	private String firstNum="0";
	private char operator='+';
	private String secondNum="0";
	private String result;
	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;
	}
	
	
	
	public void calculate(){
		BigDecimal first=new BigDecimal(this.firstNum);
		BigDecimal second=new BigDecimal(this.secondNum);
		switch(this.operator){//switch只能操作int、char类型的数据
		case'+':{
			this.result=first.add(second).toString();
			break;
		}
		case'-':{
			this.result=first.subtract(second).toString();
			break;
		}
		case'*':{
			this.result=first.multiply(second).toString();
			break;
		}
		case'/':{
			if(second.doubleValue()==0){
				throw new RuntimeException("被除数不能为0");
			}
			this.result=first.divide(second,20,BigDecimal.ROUND_HALF_UP).toString();
			break;
		}
		default:
			throw new RuntimeException("运算符只能是:+、-、*、/");
			
		}
	}

      在jsp中的代码为:

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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    
    <title>计算器</title>
    
	
  </head>
  
  <body style="text-align: center;"> <%--样式居中 --%>
  <jsp:useBean id="calculatorBean" class="cn.itcast.domain.CalculatorBean"></jsp:useBean>
  <jsp:setProperty property="*" name="calculatorBean"/>
  <%
  try{
     calculatorBean.calculate();
   }catch(Exception e){
      out.write(e.getMessage());
   }
   %>
  <br/>--------------------------------------<br/>
  计算结果是:
  <jsp:getProperty property="firstNum" name="calculatorBean"/>
  <jsp:getProperty property="operator" name="calculatorBean"/>
  <jsp:getProperty property="secondNum" name="calculatorBean"/>
  =
  <jsp:getProperty property="result" name="calculatorBean"/>
  <br/>--------------------------------------<br/>
  
  <br/>
  <form action="/day09/calculator.jsp" method="post" >
    <table width="40%" border="1">
       <tr>
        <td colspan="2">简单的计算器</td>
       </tr>
       
       <tr>
        <td>第一个参数</td>
        <td> 
         <input type="text" name="firstNum">
        </td>
       </tr>
       
       <tr>
        <td>操作符</td>
        <td>
         <select name="operator">
          <option value="+">+</option>
          <option value="-">-</option>
          <option value="*">*</option>
          <option value="/">/</option>
         
         </select>
        </td>
       </tr>
       
       <tr>
        <td>第二个参数</td>
        <td>
        <input type="text" name="secondNum">
        </td>
       </tr>
       
       <tr>
        <td>
         <input type="submit" value="计算">
        </td>
      </tr>
    </table>
    </form>
  </body>
</html>

在编写程序时需要注意的一个问题是,在进行除法计算时,由于除数不能为0,所以当除数为0时,需要抛出异常,该异常为运行时异常,并且在jsp中,需要通过jsp脚本片段将该异常捕获,给客户机一个友好提示。具体执行过程为:



展开阅读全文

C#实现计算器功能

08-02

有些功能实现不了,还有些问题,怎么实现MC,MR,MS,CE,Backspace的功能啊?rnrnrnrnrnrnrnusing System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Linq;rnusing System.Text;rnusing System.Windows.Forms;rnrnnamespace 计算器rnrn public partial class Form1 : Formrn rn char fh;rn double number, temp;rnrn rn rn public Form1()rn rn InitializeComponent();rn rnrnrn #region 输入数字rnrnrn private void button1_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "1";rn rnrn private void button2_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "2";rn rnrn private void button3_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "3";rn rnrn private void button4_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "4";rn rnrn private void button5_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "5";rn rnrn private void button6_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "6";rn rnrn private void button7_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "7";rn rnrn private void button8_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "8";rn rnrn private void button9_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "9";rn rnrn private void button0_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + "0";rn rnrnrnrn #endregionrnrnrn private void button小数点_Click(object sender, EventArgs e)rn rn richTextBox1.Text = richTextBox1.Text + ".";rn rnrn private void button正负_Click(object sender, EventArgs e)rn rn //richTextBox1.Text = richTextBox1.Text + "-";rn number = double.Parse(richTextBox1.Text);rn if (number>0)rn rn number = - number;rn richTextBox1.Text = (number).ToString();rn rn elsern rnrn richTextBox1.Text = (number).ToString();rn rn rnrn private void button百分号_Click(object sender, EventArgs e)rn rn rn number = double.Parse(richTextBox1.Text);rn //richTextBox1.Text = richTextBox1.Text + "%";rn number = number / 100;rn richTextBox1.Text = (number).ToString();rnrn rnrn private void button根号_Click(object sender, EventArgs e)rn rn //richTextBox1.Text = richTextBox1.Text+ "√";rn number = double.Parse(richTextBox1.Text);rn number =Math.Sqrt(number);rn richTextBox1.Text = number.ToString ();rnrn rn private void button分数_Click(object sender, EventArgs e)rn rn number = double.Parse(richTextBox1.Text);rn number = 1 / number;rn richTextBox1.Text = number.ToString();rn rnrnrn #region 加减乘除rnrnrnrn private void button加_Click(object sender, EventArgs e)rn rn //richTextBox1.Text = richTextBox1.Text + "+";rn number = double.Parse(richTextBox1.Text);rn fh = '+';rn richTextBox1.Text = null;rnrn rnrn private void button减_Click(object sender, EventArgs e)rn rn //richTextBox1.Text = richTextBox1.Text + "-";rn number = double.Parse(richTextBox1.Text);rn fh = '-';rn richTextBox1.Text = null;rn rnrn private void button乘_Click(object sender, EventArgs e)rn rn //richTextBox1.Text = richTextBox1.Text+ "×";rn number = double.Parse(richTextBox1.Text);rn fh = '*';rn richTextBox1.Text = null;rnrn rnrn private void button除_Click(object sender, EventArgs e)rn rn //richTextBox1.Text = richTextBox1.Text + "÷";rn number = double.Parse(richTextBox1.Text);rn fh = '/';rn richTextBox1.Text = null;rn rnrnrn #endregionrnrnrn private void button等_Click(object sender, EventArgs e)rn rn temp = double.Parse(richTextBox1.Text);rn switch (fh)rn rn case '+':rn number += temp;rn break;rn case '-':rn number -= temp;rn break;rn case '*':rn number *= temp;rn break;rn case '/':rn number /= temp;rn break;rn rn rn richTextBox1.Text = number.ToString();rnrn rnrnrn private void button1C_Click(object sender, EventArgs e)rn rn fh = ' ';rn number = 0;rn temp = 0;rn richTextBox1.Text = null; rn rnrn private void buttonCE_Click(object sender, EventArgs e)rn rnrn rnrn private void button撤销_Click(object sender, EventArgs e)rn rn this.richTextBox1.Undo();rn rn rnrn 论坛

没有更多推荐了,返回首页