首页 > 代码库 > JSP计算器
JSP计算器
<%@ page language= "java" contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta charset="utf-8">
<title>计算器</title>
</head>
<body>
<hr>
<%!
double num1 = 0.0;
double num2 = 0.0;
double result=0.0;
boolean dispaly= false;
%>
计算结果:
<%
String num3=request.getParameter("num1");
String num4=request.getParameter("num2");
String zifu = request.getParameter("zifu");
if(num3!=null&&num4!=null&&zifu!=null){
num1=Double.valueOf(num3);
num2=Double.valueOf(num4);
if("+".equals(zifu)){
result=num1+num2;
}else if("-".equals(zifu)){
result=num1-num2;
}else if("*".equals(zifu)){
result=num1*num2;
}else if("/".equals(zifu)){
result=num1/num2;
}else{
;
}
dispaly=true;
}
%>
<%if(dispaly){%>
<%=num1 %><%=zifu %><%=num2 %>=<%=result %>
<% } %>
<form action="cal.jsp" method="post">
<table border = 1 align="center">
<tr>
<td>操作数1:<input type="text" name="num1"></td>
</tr>
<tr>
<td>字符:
<select name="zifu" id="">
<option value="http://www.mamicode.com/+">+</option>
<option value="http://www.mamicode.com/-">-</option>
<option value="http://www.mamicode.com/*">*</option>
<option value="http://www.mamicode.com/">/</option>
</select>
</td>
</tr>
<tr>
<td>操作数2:<input type="text" name="num2"></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="http://www.mamicode.com/结果"></td>
</tr>
</table>
</form>
</body>
</html>
JSP计算器