jsp+session实现猜数字游戏
- .使用JSP编写页面。生成一个100之内的随机数。JSP显示一个文本框,用户输入数字。点击提交按钮。如果用户输入比随机数大了,则显示“大了”,如果用户输入比该随机数小了,则显示“小了”,如果相等,则显示“猜对了”。 可以进行多次猜测,直到正确位置(只使用JSP技术,可显示猜测次数。 提示:使用session保存数据)
源码:
NewFile.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body background="img/blue-snow.png" >
<%
session.setAttribute("message", "请您猜数");
int number =(int) (Math.random()*100)+1;
session.setAttribute("save", new Integer(number));
%>
<div style="border: 1px solid red;width: 200pt;height: 200pt">
<p>游戏规则:<br>
本游戏名为猜数游戏,系统会在你进入网站时或当你刷新网页时给出一个0到100的随机数,
你需要猜数值的大小,如果你的猜测比正确值大或小系统都会有提示,并提醒你继续猜,直到回答正确。<br>
分数计算:总分是100分,没猜一次扣两分,最后即为你的游戏分数。
</p>
</div>
<form action="NewFile1.jsp" method="post" style="text-align: center;margin-top: 50pt">
<font style="font-size: 20pt;">请输入数字:</font>
<input type="text" name = "guessnumber" size="20pt"style="height: 20pt"><br><br>
<input type="submit" value="确认" style="width: 50pt;height: 25pt;">
</form>
</body>
</html>
NewFile1.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body background="img/blue-snow.png">
<%
Integer integer = (Integer)session.getAttribute("save");
int realnumber = integer.intValue();
out.println(realnumber);
String guess = request.getParameter("guessnumber");
session.setAttribute("guessnumber", guess);
String s =(String) session.getAttribute("guessnumber");
int guessnumber = Integer.parseInt(s);
out.println( guessnumber);
int i = 0;
Object obj = session.getAttribute("i");
if(obj == null){
session.setAttribute("i", String.valueOf(i));
}else
{
i = Integer.parseInt(obj.toString());
i++;
session.setAttribute("i", String.valueOf(i));
}
if(guessnumber>realnumber)
{
request.getRequestDispatcher("猜大了.jsp").forward(request, response);
}
else if(guessnumber<realnumber)
{
request.getRequestDispatcher("猜小了.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("success.jsp").forward(request, response);
}
%>
</body>
</html>
Success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body background="img/blue-snow.png">
<font style="font-size: 25pt">恭喜你,答对了!</font>
<%
String s = (String)session.getAttribute("i");
int in = Integer.parseInt(s);
int count = 100-(2*in);
session.setAttribute("count", String.valueOf(count));
%>
<h3 style="color: red;">答案就是:<%=session.getAttribute("save") %>;</h3>
<h3>你的最终分数是:<%=session.getAttribute("count") %></h3>
</body>
</html>
猜小了.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body background="img/blue-snow.png">
<form action="NewFile1.jsp" method="post" style="text-align: center;margin-top: 50pt">
<font style="font-size: 20pt;">猜小了,重新输入:</font>
<input type="text" name = "guessnumber" size="20pt"style="height: 20pt"><br><br>
<input type="submit" value="确认" style="width: 50pt;height: 25pt;">
</form>
<font>你猜的数是:</font>
<%=session.getAttribute("guessnumber") %>
</body>
</html>
猜大了.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body background="img/blue-snow.png">
<form action="NewFile1.jsp" method="post" style="text-align: center;margin-top: 50pt">
<font style="font-size: 20pt;">猜大了,重新输入:</font>
<input type="text" name = "guessnumber" size="20pt"style="height: 20pt"><br><br>
<input type="submit" value="确认" style="width: 50pt;height: 25pt;">
</form>
<font>你猜的数是:</font>
<%=session.getAttribute("guessnumber") %>
</body>
</html>
截图: