日期:2014-05-17 浏览次数:20859 次
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>GuessNumber</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table width="200" height="100" align="center" border="0" bgColor="#0080ff"> <tr> <td> 请猜一个0~9的数字: </td> </tr> <tr> <td> <input type="text" maxlength="1" name="guess"> </td> <td><%=session.getAttribute("answer")%></td> </tr> <tr> <td><%=session.getAttribute("count")%></td> <td> <input type="submit" value="确定" name="ok"> </td> </tr> </table> <% String Guess = new String(request.getParameter("guess").getBytes( "ISO_8859_1"), "GBK"); int n = 3; int num = (int) Math.random() * 10; String count = n + "chances!"; String answer = null; session.setAttribute("count", count); while (n != 0) { if (Guess != null) { int guess = Integer.parseInt(Guess); if (num == guess) { answer = "Congratulation!"; session.setAttribute("answer", answer); break; } else { n--; answer = "Sorry!You are wrong!"; session.setAttribute("answer", answer); count = n + "chances!"; session.setAttribute("count", count); } } else { answer = "Please input a number!"; session.setAttribute("answer", answer); count = n + "chances!"; session.setAttribute("count", count); } } %> </body> </html>