共计 2904 个字符,预计需要花费 8 分钟才能阅读完成。
游戏要求:
代码实现:
c1_caishu_.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>
请输出一个 1~20 的整数进行猜数!
<%
int num = (int)Math.random()*20+1;
session.setAttribute("count", new Integer(0));// 猜数的次数
session.setAttribute("randomNum", new Integer(num));// 产生的随机数
%>
<form action="c1_caishu_judge.jsp" method="post">
<input type="text" name="guess">
<input type="submit" name="tijiao" value="提交">
</form>
</body>
</html>
c1_caishu_judge.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>
<%
String str = request.getParameter("guess");//request.getParameter 返回的是 String 类型
if(str==null && str.length()==0){response.sendRedirect("c1_caishu_.jsp");
}else{int userGuessNum = Integer.parseInt(str);// 用户猜的数
session.setAttribute("guessNum", new Integer(userGuessNum));
Integer randomNum = (Integer)session.getAttribute("randomNum");// 生成的随机数
if(userGuessNum == randomNum.intValue()){// 猜对了
int n = ((Integer)session.getAttribute("count")).intValue();// 猜数的次数
n++;
session.setAttribute("count", new Integer(n));
response.sendRedirect("c1_caishu_success.jsp");
}else if(userGuessNum > randomNum.intValue()){// 猜大了
int n = ((Integer)session.getAttribute("count")).intValue();// 猜数的次数
n++;
session.setAttribute("count", new Integer(n));
response.sendRedirect("c1_caishu_large.jsp");
}else{ // 猜小了
int n = ((Integer)session.getAttribute("count")).intValue();// 猜数的次数
n++;
session.setAttribute("count", new Integer(n));
response.sendRedirect("c1_caishu_small.jsp");
}
}
%>
</body>
</html>
c1_caishu_large.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>
<%
int guessNum = Integer.parseInt(session.getAttribute("guessNum").toString());// 用户猜的数
%>
<%=guessNum %> 数大了, 请再猜:
<form action="c1_caishu_judge.jsp" method="post">
<input type="text" name="guess">
<input type="submit" name="tijiao" value="送出">
</form>
</body>
</html>
c1_caishu_small.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>
<%
Integer guessNum = (Integer)session.getAttribute("guessNum");// 用户猜的数
%>
<%=guessNum %> 数小了, 请再猜:
<form action="c1_caishu_judge.jsp" method="post">
<input type="text" name="guess">
<input type="submit" name="tijiao" value="送出">
</form>
</body>
</html>
c1_caishu_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>
<%
Integer guessNum = (Integer)session.getAttribute("guessNum");// 用户猜的数
Integer count = (Integer)session.getAttribute("count");// 用户猜的次数
%>
祝贺你猜对了!!!<br><br>
你猜的数为:<%=guessNum %><br><br>
你一共猜了 <%=count %> 次 <br><br>
再来猜一次? <a href="c1_caishu_.jsp"> 好 </a>
</body>
</html>
正文完