简单的计算器

完成两个操作数的运算,实现简单计数器的功能:

 

Index.html

<html>

  <head>  

        <title>计算器</title>   

  </head> 

 

  <body>

    <form action="calculate.jsp" method="post">

      

  <table width="60%" border="1" align="center">

    <tr>

      <td colspan="2"><div align="center">计算器</div></td>

    </tr>

    <tr>

      <td width="40%">第一个参数</td>

      <td width="60%"><input type="text" name="first" ></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="second" ></td>

    </tr>

    <tr>

      <td colspan="2"><div align="center">

          <input type="submit" name="Submit" value="提交">

        </div></td>

    </tr>

  </table>

</form>   

  </body>

</html>

 

calculate.jsp

<%@ page language="java" import="java.util.*" contentType="text/html;charset=gb2312"%>

<!--   这里是声明,声明一个方法,用于计算两个操作数 -->

<%!

    double result=0.0;   

    double calculate(String first,String second,String oper){

      double one=Double.parseDouble(first);

      double two=Double.parseDouble(second);

      try{

         if(oper.equals("+")) result=one+two;

         else if(oper.equals("-")) result=one-two;

           else if(oper.equals("*")) result=one*two;

             else if(oper.equals("/")) result=one/two;            

       

      }catch(Exception ex){

          System.out.println(ex.getMessage());

      }

        return result;

    }

%>

<html>

  <head><title>计算器</title> </head>

  <body>

  <!--这里是使用JSP脚本代码段,把计算结果打印输出 -->

  <%

      String first=request.getParameter("first");

      String second=request.getParameter("second");

      String oper=request.getParameter("operator");

      double res=calculate(first,second,oper);     

      %>

   <H2> <p align="center">计算结果为:

    <%=first%>

     <%=oper%>

     <%=second%>=<%=res%>

     </p>

    </H2> 

  </body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值