avatar

猜数游戏作业

jsp的猜数游戏作业

guess1.jsp

<%@ page import="java.util.Random" %><%--
Created by IntelliJ IDEA.
User: 行路
Date: 2020/4/13
Time: 19:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>guess1</title>

</head>
<body>
<%
Random random=new Random();
int i = random.nextInt(100) + 1;
session.setAttribute("rand",i);
int count=0;
session.setAttribute("count",count);
long time = System.currentTimeMillis();
session.setAttribute("time",time);
response.sendRedirect("Guess.jsp");
%>
</body>
</html>

Guess.jsp

<%@ page import="java.util.Random" %><%--
Created by IntelliJ IDEA.
User: 行路
Date: 2020/4/13
Time: 18:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>猜数游戏</title>
</head>
<body>
<%
int i = (int) session.getAttribute("rand");
int count = (int) session.getAttribute("count");
long currentTimeMillis = 0;
System.out.println(i);
if (request.getParameter("guess") != "" && request.getParameter("guess") != null) {
int guess = Integer.parseInt(request.getParameter("guess"));
if (guess > i) {
session.setAttribute("guess", "猜大了");
count++;
session.setAttribute("count", count);
} else if (guess < i) {
session.setAttribute("guess", "猜的太小了");
count++;
session.setAttribute("count", count);
} else {
session.setAttribute("guess", "猜数正确");
currentTimeMillis = System.currentTimeMillis();
count++;
session.setAttribute("count", count);
}
} else {
session.setAttribute("guess", "输入的数不能为空");
}
%>
<form action="Guess.jsp" method="post" >
<p><%
if (session.getAttribute("guess") != null) {
out.write(session.getAttribute("guess").toString());
if (session.getAttribute("guess").toString().equals("猜数正确")) {
out.write("共猜了" + (int) session.getAttribute("count") + "次,耗时" + (currentTimeMillis - (Long) session.getAttribute("time")) / 1000 + "秒");
}
}
%>
</p>
<h2>猜数游戏</h2>
<p>请输入1-100的数字</p>
<input type="text" name="guess"/>
<%
if (session.getAttribute("guess") != null && session.getAttribute("guess").toString().equals("猜数正确")) {
session.removeAttribute("guess");
%>
<button type="submit" disabled>提交</button>
<% } else {%>
<button type="submit">提交</button>
<%}%>
</form>
</body>
</html>
文章作者: Todcsw
文章链接: https://todcsw.github.io/2020/04/13/%E7%8C%9C%E6%95%B0%E6%B8%B8%E6%88%8F/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 行路のblog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论