项目预览
实现代码
①/ybq/WebContent/guess/input.jsp
body{
position: absolute;
}
input{
border:1px solid #000;
margin: auto;
}
String message = (String) session.getAttribute("message");
if (message != null)
out.println(message);
if(session.isNew()||"您猜对了,系统将重新分配随机数".equals(message))
session.setAttribute("randomNumber", (int)(Math.random() * 100));
%>
欢迎来玩猜数字—— |
请输入0-99的随机数字: |
type="reset" value="撤销"> |
②/ybq/WebContent/guess/compare.jsp
request.setCharacterEncoding("UTF-8");
String userNum = request.getParameter("userNumber");
int userNumber = (Integer) Integer.parseInt(userNum);
int randomNumber = (Integer)session.getAttribute("randomNumber");
if (userNumber <= 99 && userNumber >= 0) {
if (userNumber == randomNumber) {
session.setAttribute("message", "您猜对了,系统将重新分配随机数");
session.removeAttribute("randomNumber");
response.sendRedirect("input.jsp");
}
if (userNumber > randomNumber) {
session.setAttribute("message", "你输入的是"+userNumber+",您猜大了,请重新输入" + randomNumber);
response.sendRedirect("input.jsp");
}
if (userNumber < randomNumber) {
session.setAttribute("message", "你输入的是"+userNumber+",您猜小了,请重新输入" + randomNumber);
response.sendRedirect("input.jsp");
}
} else {
session.setAttribute("message", "输入的数不符合规格,请重新输入");
response.sendRedirect("input.jsp");
}
%>
运行结果