jsp用request对象设计实现一个简单的计算加减乘除运算的网页程序
时间: 2023-05-24 11:01:05 浏览: 130
可以通过request对象获取输入的两个数和运算符,然后根据运算符进行相应的计算。以下是一个简单的示例代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>计算器</title>
</head>
<body>
<form method="post">
<input type="number" name="num1">
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" name="num2">
<input type="submit" value="计算">
</form>
<%
if(request.getMethod().equalsIgnoreCase("post")) {
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));
String operator = request.getParameter("operator");
int result;
switch(operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
default:
result = 0;
}
out.print(num1 + " " + operator + " " + num2 + " = " + result);
}
%>
</body>
</html>
阅读全文