jsp正確使用session完成猜數遊戲(完整版!!! 優化版!!!)
阿新 • • 發佈:2021-06-30
遊戲要求:
程式碼實現:
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的整數進行猜數! <% intnum = (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>