首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

最近在做一个实验,不过出现了有关问题解决不了,求高手帮帮忙,感激不尽啊

2013-09-11 
最近在做一个实验,不过出现了问题解决不了,求高手帮帮忙,感激不尽啊。。。题目是:猜数字游戏,程序生成一个0~9

最近在做一个实验,不过出现了问题解决不了,求高手帮帮忙,感激不尽啊。。。
题目是:猜数字游戏,程序生成一个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");


54: int n = 3;
55: int num = (int) Math.random() * 10;
56: String count = n + "chances!";


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause?

java.lang.NullPointerException
org.apache.jsp.GuessNumber_jsp._jspService(GuessNumber_jsp.java:115)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.


[解决办法]
我随便写了个你的例子,不知道满足你的需求么。参考下楼主。

HTML code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%    Random r=new Random(); //实例化一个Random类    //随机产生一个大于等于0小于10的整数    int answer=r.nextInt(10);    System.out.println("随机产生一个大于等于0小于10的整数:"+answer);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>    <head>        <title>GuessNumber</title>        <script type="text/javascript">        var count=0;        var is_correct=false;        var answer=<%=answer%>;        function check(){            var num=document.getElementById("inputNumber").value;            if(num==''){                        }else{                if(count<=2&&is_correct==false){                    count++;                    if(num==answer){                        alert('恭喜您,猜对了!');                            is_correct=true;                    }else{                        alert('不好意思,您猜错了,您还有'+(3-count)+'次机会,请重试!')                    }                    }                                            }        }    </script>    </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" id="inputNumber">                </td>            </tr>            <tr>                <td>                    <input type="submit" value="确定" name="checkNumber"                        onclick="check()">                </td>            </tr>        </table>    </body></html>
[解决办法]
13楼给了一个用JavaScript在页面端进行猜数判断的样例,挺好的。

这里是服务端进行猜数判断的(没做太多差错控制):
HTML code
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="java.util.*"%><%response.setHeader("Cache-Control", "no-cache, no-store"); //HTTP 1.1  response.setHeader("Pragma", "no-cache"); //HTTP 1.0  response.setDateHeader("Expires", 0); //prevents caching at the proxy server// 定义变量int MAX = 3;//从提交表单(request)得到用户提交的猜测数String strGuess = request.getParameter("guess");Integer guess = -1;try {    guess = Integer.valueOf(strGuess);} catch (Exception ex) {}//从Session得到猜测次数,没有说明是第一次 Integer times = (Integer) session.getAttribute("times");if (times == null || times >= MAX) {    times = 0;}if (guess >= 0) {    times++;}session.setAttribute("times", times);// 从Session得到随机数,没有则生成Integer num = (Integer) session.getAttribute("num");if (times == 0 || num == null) {    num = (int)(Math.random() * 10);    session.setAttribute("num",num);}boolean correct = guess.equals(num); // 猜中否?if (correct) session.removeAttribute("times");%><html><body><%if (guess >= 0) {    out.write("第"+times+"次,您猜测的数字是:" + guess);    if (correct) out.write(",恭喜,正确了!请继续输入以重来。");    else if (times < 3) out.write(",不正确。");    else out.write(",不正确。正确答案是:" + num + "。请继续输入以重来。");}%><p></p>    <form method="post">        请输入猜测数(0~9):        <input type="text" name="guess">        <input type="submit">    </form></body></html> 


我的异常网推荐解决方案:An exception occurred processing JSP page,http://www.myexception.cn/j2se/33144.html
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html

热点排行