简易计算器

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<%!String result = ""; %>
<%
result = "计算结果:";
String first =request.getParameter("first");
String second = request.getParameter("second");
String select = request.getParameter("select");
if(first != null && first.length()!= 0 && second != null && second.length() != 0){
if(select.equals("add")){
result += Double.parseDouble(first) + "+" +Double.parseDouble(second)+"="+(Double.parseDouble(first)+Double.parseDouble(second));
}else if(select.equals("minus")){
result += Double.parseDouble(first) + "-" +Double.parseDouble(second)+"="+(Double.parseDouble(first)-Double.parseDouble(second));
}else if(select.equals("multiply")){
result += Double.parseDouble(first) + "*" +Double.parseDouble(second)+"="+(Double.parseDouble(first)*Double.parseDouble(second));
}else if(select.equals("divide")){
result += Double.parseDouble(first) + "÷" +Double.parseDouble(second)+"="+(Double.parseDouble(first)/Double.parseDouble(second));
}
}
%>
<form action="Calculator.jsp" method="post">
<hr>
<%=result%>
<%
result= "计算结果:";
%>
<hr>
<table border="1">
<tr>
<td colspan="2">简单的计算器</td>
</tr>
<tr>
<td>第一个操作数</td>
<td><input type="text" name="first"></td>
</tr>
<tr>
<td>操作符</td>
<td>
<select name="select">
<option value="add">+</option>
<option value="minus">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
</td>
</tr>
<tr>
<td>第二个操作数</td>
<td><input type="text" name="second"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="计算"></td>
</tr>
</table>
</form>
</body>
</html>

原文地址:https://www.cnblogs.com/tcc89/p/5599013.html