小小游戏
程序 c.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body bgcolor = "#930047"><font size = 5> 随机分配给你一个1-100之间的数字,请猜! <% int i = (int)(Math.random()*100+1); session.setAttribute("count",new Integer(0)); session.setAttribute("save",new Integer(i)); %> <BR><A HREF = "d.jsp">去猜这个数</A> </font> </body> </html> d.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <%@ taglib tagdir="/WEB-INF/tags" prefix="guessNumber"%> <body bgcolor = cyan> <font size=3 color="red"> <% String str = request.getParameter("guessNumber"); if(str==null) str="*"; if(str.length()==0) str="*"; %> <guessNumber:GuessTag number="<%=str %>" /> 当前猜测的结果是:<%=message %> <% if(message.startsWith("您猜对了")){%> <br> <a href="c.jsp">重新获得随机数</a> <%} else{ %> <form action="" method=post name=form> <input type="text" name="guessNumber"> <input type="submit" name="送出" name="submit"> </form> <%}%> </font> </body> </html> GuessTag.tag <%@ tag language="java" pageEncoding="UTF-8"%> <%@ attribute name="number" required="true"%> <%@ variable name-given="message" scope="AT_END"%> <% String mess = ""; Integer integer = (Integer)session.getAttribute("save"); int realnumber = integer.intValue(); int guessNumber = 0; boolean boo = true; try{ guessNumber = Integer.parseInt(number); } catch(Exception e){ boo = false; } if(boo){ if(guessNumber==realnumber){ int n = ((Integer)session.getAttribute("count")).intValue(); n=n+1; session.setAttribute("count",new Integer(n)); mess = "您猜对了,这是第"+n+"次猜测"; } else if(guessNumber>realnumber){ int n = ((Integer)session.getAttribute("count")).intValue(); n=n+1; session.setAttribute("count",new Integer(n)); mess = "您猜大了,这是第"+n+"次猜测"; } else if(guessNumber<realnumber){ int n = ((Integer)session.getAttribute("count")).intValue(); n=n+1; session.setAttribute("count",new Integer(n)); mess = "您猜小了,这是第"+n+"次猜测"; } else if(number.equals("你还没开始猜测")){ mess = "你还没开始猜测"; } jspContext.setAttribute("message",mess); }else { jspContext.setAttribute("message","请输入你的猜测"); } %>