ajax进行四则运算
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CountControl extends HttpServlet {


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String number1=request.getParameter("number1");
        String number2=request.getParameter("number2");
        String operator=request.getParameter("operator");
        int operate=Integer.valueOf(operator);
        Double num1=Double.valueOf(number1);
        Double num2=Double.valueOf(number2);
        Double result10=0.0d;
        Double result4=0.0d;
        String responseMsg="";
        switch(operate){
        case 1:
             result10=add(num1,num2);
             responseMsg="加法运算结果:"+String.valueOf(result10);
             break;
        case 2:
             result10=sub(num1,num2);
             responseMsg="减法运算结果:"+String.valueOf(result10);
             break;
        case 3:
                result10=mul(num1,num2);
                responseMsg="乘法运算结果:"+String.valueOf(result10);
                break;
        case 4:
                result10=div(num1,num2,10);
                result4=div(num1,num2,4);
                responseMsg="除法运算结果:"+String.valueOf(result10)+"</br>除法运算结果保留4位小数:"+String.valueOf(result4);
                break;
            
        }
        
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(responseMsg);
        
        

    }

    /**
     * 两个Double数相加
     * @param v1
     * @param v2
     * @return Double
     */
    public static Double add(Double v1,Double v2){
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.add(b2).doubleValue();
    }
    
    /**
     * 两个Double数相减
     * @param v1
     * @param v2
     * @return Double
     */
    public static Double sub(Double v1,Double v2){
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.subtract(b2).doubleValue();
    }
    
    /**
     * 两个Double数相乘
     * @param v1
     * @param v2
     * @return Double
     */
    public static Double mul(Double v1,Double v2){
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.multiply(b2).doubleValue();
    }
    
    /**
     * 两个Double数相除
     * @param v1
     * @param v2
     * @return Double
     */
    public static Double div(Double v1,Double v2,int scale){
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}








<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
        <h1>计算器</h1>
        <p>数字一:<input type="text" id="number1" style="width: 100px;" value=""/></p>
        <p>
            <label><input type="radio" name="symbol" value="1" checked="checked"/>+</label>
            <label><input type="radio" name="symbol" value="2" />-</label>
            <label><input type="radio" name="symbol" value="3" />*</label>
            <label><input type="radio" name="symbol" value="4" />/</label>
        </p>
        <p>数字二:<input type="text" id="number2" style="width: 100px;" value=""/></p>
        <p><button>计算</button></p>
        <h2>结果:<span></span></h2>
    </body>



<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
        $(function(){
            $("button").click(function(){
                var number1  = $("#number1").val();
                var number2 = $("#number2").val();
                var symbol = parseInt($("[type=radio]:checked").val());//获取符号的值
                if(number1==null||number1==""){
                        alert("请输入第一个数!");
                        return;
                }
                if(number2==null||number2==""){
                        alert("请输入第二个数!");
                        return;
                }
                if(symbol==4&&(number2=="0")){
                        alert("分母不能为0!");
                        return;
                }
                   $.ajax({
                       type:"post",//请求方式
                       url:"/ajax/servlet/CountControl",//发送请求地址
                       data:{//发送给数据库的数据
                       number1:number1,
                       number2:number2,
                       operator:symbol
                       },
                       //请求成功后的回调函数有两个参数
                       success:function(data,textStatus){
                        $("h2 span").html(data);
                       }
                   });
               
            })         
        })
    </script>
</html>


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35522169/article/details/80319713
想对作者说点什么? 我来说一句

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

不良信息举报

ajax进行四则运算

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭