运用Session的猜数字游戏

来源:互联网 发布:mmd绅士动作数据 编辑:程序博客网 时间:2024/06/06 00:05

运用session保存数据


guess1.jsp

<body>   <a href="guess2.jsp">去玩猜数字游戏</a>   <%   int i=(int)(Math.random()*100+1);   session.setAttribute("number", 1);   session.setAttribute("count", 0);    %>  </body>

guess2.jsp

 <body>   请输入你猜的数字:   <form action="guess3.jsp">   <input type="text" name="n">   <input type="submit" value="guess">      </form>  </body>

guess3.jsp

 <body>  <%  String str=request.getParameter("n");  int number=Integer.parseInt(str);  int trueN=(Integer)session.getAttribute("number");  int count=(Integer)session.getAttribute("count");  count++;  session.setAttribute("count", count);  if(number>trueN){  out.print("猜大了,这是第"+count+"次,返回继续----》");  out.print("<a href='guess2.jsp'>=====</a>");  }else if(number>trueN){  out.print("猜小了,这是第"+count+"次,返回继续----》");  out.print("<a href='guess2.jsp'>=====</a>");  }else{  out.print("猜对了,一共花了"+count+"次");  }     %>    </body>


0 0