LZ,这是调试后达到你想要的效果吧?关键部分已经对其注释说明。
[Java] 纯文本查看 复制代码 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>输入数字</title>
</head>
<body>
<%
//第一次进入该页面时字段isEnd不存在,此时需要初始化save、count、isEnd
if(session.getAttribute("isEnd") == null){
int number=(int)(Math.random()*100)+1;
session.setAttribute("count", 0);
session.setAttribute("save", number);
session.setAttribute("isEnd", 0);
}
//获取字段isEnd,用于判断是否猜测正确(isEnd=1)
int isEnd = (int)session.getAttribute("isEnd");
//获取字段count,当前猜测的次数
int currentCount = (int)session.getAttribute("count");
//获取字段save,当前猜测的数
int currentSave = (int)session.getAttribute("save");
if(isEnd != 1){
String currentNum = session.getAttribute("numb")==null?"":(String)session.getAttribute("numb");
//第一次进入该页面时不显示该提示,只有猜测过了才显示当前提示语
if(currentNum != null && !currentNum.equals("")){
String msg = "";
if(Integer.parseInt(currentNum) < currentSave){
msg = "小了";
}else{
msg = "大了";
}
%>
<div>当前输入的数字:<%=currentNum%>,次数为<%=currentCount%>次,<%=msg%>,
请再次输入</div>
<%
}
%>
<%
}else{
//猜测正确时提示
%>
<div>恭喜您猜对了,当前的数字:<%=currentSave%>,
猜测了<%=currentCount%>次,再次提交将重新猜测</div>
<%
}
%>
<form action="${pageContext.request.contextPath}/guess.jsp" method="post">
<input type="text" name="numb" placeholder="请输入1~100的整数">
<input type="submit" value="提交">
</form>
<%
//当猜测正确时需要把count置为0,save重新随机
if(isEnd == 1){
int number=(int)(Math.random()*100)+1;
session.setAttribute("count", 0);
session.setAttribute("save", number);
}
%>
</body>
</html>
[Java] 纯文本查看 复制代码 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>猜数字</title>
</head>
<body>
<%
String numb = request.getParameter("numb");
if (numb == null) {
numb = "0";
}
int guessNumber = Integer.parseInt(numb);
int realnumber = (Integer) session.getAttribute("save");
if (guessNumber == realnumber) {
session.setAttribute("isEnd", 1);
// int n = (Integer) session.getAttribute("count");
// n = n + 1;
// session.setAttribute("count", n);
// response.sendRedirect("guess.jsp");
} else if (guessNumber > realnumber) {
session.setAttribute("isEnd", 0);
// int n = (Integer) session.getAttribute("count");
// n = n + 1;
// session.setAttribute("count", n);
// response.sendRedirect("guess.jsp");
} else {
session.setAttribute("isEnd", 0);
// int n = (Integer) session.getAttribute("count");
// n = n + 1;
// session.setAttribute("count", n);
// response.sendRedirect("guess.jsp");
}
//保存每次猜测的数字,方便index.jsp展示
session.setAttribute("numb", numb);
//不管是怎样的判断,都需要对猜测次数进行累加并保存
int n = (Integer) session.getAttribute("count");
n = n + 1;
session.setAttribute("count", n);
//重定向到猜测页面,再次猜测
response.sendRedirect("index.jsp");
%>
</body>
</html>
PS:当前是把生成随机数放置在第一个页面,同时又需要可以打印并继续猜测,代码会显得略繁琐复杂,最好是把生成猜测随机数放置在第二个页面,第一个页面只做展示&输入,不涉及核心操作(如生成随机数),改动后如下:
[Java] 纯文本查看 复制代码 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>输入数字</title>
</head>
<body>
<%=session.getAttribute("msg")==null?"":session.getAttribute("msg")%>
<form action="${pageContext.request.contextPath}/guess.jsp" method="post">
<input type="text" name="numb" placeholder="请输入1~100的整数">
<input type="submit" value="提交">
</form>
<%
//int number=(int)(Math.random()*100)+1;
//session.setAttribute("count", 0);
//session.setAttribute("save", number);
%>
</body>
</html>
[Java] 纯文本查看 复制代码 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>猜数字</title>
</head>
<body>
<%
String numb = request.getParameter("numb");
if (numb == null) {
numb = "0";
}
int guessNumber = Integer.parseInt(numb);
//第一次需要初始化随机数
if(session.getAttribute("save") == null){
int number=(int)(Math.random()*100)+1;
session.setAttribute("count", 0);
session.setAttribute("save", number);
}
int realnumber = (Integer) session.getAttribute("save");
//保存每次猜测的数字,方便index.jsp展示
session.setAttribute("numb", numb);
//不管是怎样的判断,都需要对猜测次数进行累加并保存
int n = (Integer) session.getAttribute("count");
n = n + 1;
session.setAttribute("count", n);
String msg = "";
if (guessNumber == realnumber) {
// int n = (Integer) session.getAttribute("count");
// n = n + 1;
// session.setAttribute("count", n);
// response.sendRedirect("guess.jsp");
msg = "恭喜您猜对了,当前的数字:" + realnumber + ",总共猜测了"+ session.getAttribute("count") + "次,,再次提交将重新猜测!";
//猜测正确时需要重新初始化随机数
int number=(int)(Math.random()*100)+1;
session.setAttribute("count", 0);
session.setAttribute("save", number);
} else if (guessNumber > realnumber) {
// int n = (Integer) session.getAttribute("count");
// n = n + 1;
// session.setAttribute("count", n);
// response.sendRedirect("guess.jsp");
msg = "当前输入的数字:"+guessNumber+",大了,次数为"+session.getAttribute("count")+",请再次输入";
} else {
// int n = (Integer) session.getAttribute("count");
// n = n + 1;
// session.setAttribute("count", n);
// response.sendRedirect("guess.jsp");
msg = "当前输入的数字:"+guessNumber+",小了,次数为"+session.getAttribute("count")+",请再次输入";
}
session.setAttribute("msg", msg);
//重定向到猜测输入页面,再次猜测
response.sendRedirect("index.jsp");
%>
</body>
</html>
|