<%@ 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>
|