我正在编写一个执行计算的简单网络应用程序。我有 2 个按钮,用于加法和减法,但它们不起作用。我尝试使用一个switch
语句和一个if
语句,但都没有工作。你能帮我理解这个问题吗?
这是我的代码...
<%
String name = (String) session.getAttribute("name");
if (name==null) {
name = request.getParameter("name");
if (name!=null){
session.setAttribute("name",name);
}
}
String sum = (String) session.getAttribute("sum");
if (sum==null) {
sum = "0";
session.setAttribute("sum",sum);
}
int isum = Integer.parseInt(sum);
String number = request.getParameter("number");
if (number==null){
number = "0";
}
int inumber = Integer.parseInt(number);
if(request.getParameter("add") != null){
isum += inumber;
session.setAttribute("sum",""+isum);
}
if(request.getParameter("subtract") != null){
isum += inumber;
session.setAttribute("sum",""+isum);
}
%>
<html>
<head>
<title>Adding Machine</title>
</head>
<body>
<form method='get' action='adder.jsp'>
<%
if (name==null) {
%>
<p>
Name: <input type='text' name='name' id='name'>
</p>
<p>
<input type='submit' name='submit' id='submit' value='signin'>
</p>
<%
} else {
%>
<p>Welcome, <%=name%>!</p>
<p>Current sum is: <%=isum%></p>
<p>
Number to add: <input type='text' name='number' id='number' value='add'>
</p>
<p>
<input type='submit' name='submit' id='submit' value='add'>
</p>
<p>
<input type='submit' name='submit' id='submit' value='subtract'>
</p>
<%
}
%>
</form>
</body>
</html>