题目是:猜数字游戏,程序生成一个0~9随机数,玩家有三次机会猜,三次机会之内猜中了提示猜对了,否则提示正确答案并询问是否继续游戏。
下面是我的代码,我感觉会有挺多问题的,但是不知道怎么改,求高手帮忙。。。。
- Java code
<%@ 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>
运行时出现的问题是:
type Exception report
message?
description The server encountered an internal error () that prevented it from fulfilling this request.
exception?
org.apache.jasper.JasperException: An exception occurred processing JSP page /GuessNumber.jsp at line 53
50:?
51: <%
52: String Guess = new String(request.getParameter("guess").getBytes(
53: "ISO_8859_1"), "GBK");