Game requirements:
Code implementation:
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> <% 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=" submit "> </form> </body> </ HTML >Copy the code
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 returns 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(); // the number of guess 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(); // the number of guess n++; session.setAttribute("count", new Integer(n)); response.sendRedirect("c1_caishu_large.jsp"); }else{// guess small int n = ((Integer)session.getAttribute("count")).intValue(); // the number of guess n++; session.setAttribute("count", new Integer(n)); response.sendRedirect("c1_caishu_small.jsp"); } } %> </body> </html>Copy the code
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 %> <%=guessNum %> <form action="c1_caishu_judge.jsp" method="post"> <input type="text" name="guess"> <input type="submit" name="tijiao" Value =" send "> </form> </body> </ HTML >Copy the code
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 %> <%=guessNum %> <form action="c1_caishu_judge.jsp" method="post"> <input type="text" name="guess"> <input type="submit" name="tijiao" Value =" send "> </form> </body> </ HTML >Copy the code
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"); // Number of guesses by the user Integer count = (Integer)session.getAttribute("count"); // The number of user guesses %> Congratulations you guessed right!! < br > < br > can you guess what the number is: < % = guessNum % > < br > < br > you guessed % > < % = count time < br > < br > to guess again? <a href="c1_caishu_.jsp"> Good </a> </body> </ HTML >Copy the code