JSP猜数字页面 不能正常输出判断结果 显示的是一串问号 为什么
这是页面显示的结果(问号处本来应该是显示“猜大了”“猜小了”的)我用一个JAVAbean负责判断javaBean程序如下packagetom.jiafei;importj...
这是页面显示的结果(问号处本来应该是显示“猜大了 ”“猜小了” 的)
我用一个JAVAbean 负责判断 javaBean 程序如下
package tom.jiafei;
import java.util.*;
public class GuessNumber{
int answer=0,guessNumber=0,guessCount=0;String result=null;
boolean right=false;
public void setAnswer(int n){
answer=n;guessCount=0;
}
public int getAnswer(){
return answer;
}
public void setGuessNumber(int n){
guessNumber=n;
guessCount++;
if(guessNumber==answer){
result="恭喜,猜对了";
right=true;
}
else if(guessNumber>answer){
result="猜大了";
right=false;
}
else if(guessNumber<answer){
result="猜小了";
right=false;
}
}
public int getGuessNumber(){
return guessNumber;
}
public int getGuessCount(){
return guessCount;
}
public String getResult(){
return result;
}
public boolean isRight(){
return right;}
}
获取用户猜测数的JSP代码是:getNumber.jsp
<%@page contentType="text/html;Charset=GB2312"%><%@page import="tom.jiafei.GuessNumber"%>
<HTML><BODY>
<%int n=(int)(Math.random()*100)+1;%>
<jsp:useBean id="guess" class="tom.jiafei.GuessNumber" scope="session"/>
<jsp:setProperty name="guess" property="answer" value="<%=n%>"/>
<p>随机给你一个1到100之间的数,试猜测这个数是多少
<%String str=response.encodeRedirectURL("guess.jsp");%>
<FORM action="<%=str%>" method=post>
<BR>输入你的猜测:<input type=text name="guessNumber">
<input type=submit value="提交">
</FORM></BODY>
输出结果的页面代码是:
<%@ page contentType="text/html;Charset=GB2312"%>
<%@ page import="tom.jiafei.GuessNumber"%>
<% String strGuess=response.encodeRedirectURL("guess.jsp");
String strGetNumber=response.encodeRedirectURL("getNumber.jsp"); %>
<HTML><BODY>
<jsp:useBean id="guess" class="tom.jiafei.GuessNumber" scope="session"/>
<jsp:setProperty name="guess" property="guessNumber" param="guessNumber"/>
<BR> <jsp:getProperty name="guess" property="result"/>这是第
<jsp:getProperty name="guess" property="guessCount"/>次猜
你给出的数是<jsp:getProperty name="guess" property="guessNumber"/>
<%if(guess.isRight()==false){%>
<FORM action="<%=strGuess%>" method=post>
再输入你的猜测:<input type=text name="guessNumber">
<input type=submit value="提交">
</FORM>
<% } %>
<BR><A href="<%=strGetNumber%>">链接到getnumber.jsp重新玩猜数</A>
</BODY></HTML>
检查了很多次找不出原因所以把三个代码都写上了 有点多 请会的前辈帮忙运行下 找出错误 谢啦 展开
我用一个JAVAbean 负责判断 javaBean 程序如下
package tom.jiafei;
import java.util.*;
public class GuessNumber{
int answer=0,guessNumber=0,guessCount=0;String result=null;
boolean right=false;
public void setAnswer(int n){
answer=n;guessCount=0;
}
public int getAnswer(){
return answer;
}
public void setGuessNumber(int n){
guessNumber=n;
guessCount++;
if(guessNumber==answer){
result="恭喜,猜对了";
right=true;
}
else if(guessNumber>answer){
result="猜大了";
right=false;
}
else if(guessNumber<answer){
result="猜小了";
right=false;
}
}
public int getGuessNumber(){
return guessNumber;
}
public int getGuessCount(){
return guessCount;
}
public String getResult(){
return result;
}
public boolean isRight(){
return right;}
}
获取用户猜测数的JSP代码是:getNumber.jsp
<%@page contentType="text/html;Charset=GB2312"%><%@page import="tom.jiafei.GuessNumber"%>
<HTML><BODY>
<%int n=(int)(Math.random()*100)+1;%>
<jsp:useBean id="guess" class="tom.jiafei.GuessNumber" scope="session"/>
<jsp:setProperty name="guess" property="answer" value="<%=n%>"/>
<p>随机给你一个1到100之间的数,试猜测这个数是多少
<%String str=response.encodeRedirectURL("guess.jsp");%>
<FORM action="<%=str%>" method=post>
<BR>输入你的猜测:<input type=text name="guessNumber">
<input type=submit value="提交">
</FORM></BODY>
输出结果的页面代码是:
<%@ page contentType="text/html;Charset=GB2312"%>
<%@ page import="tom.jiafei.GuessNumber"%>
<% String strGuess=response.encodeRedirectURL("guess.jsp");
String strGetNumber=response.encodeRedirectURL("getNumber.jsp"); %>
<HTML><BODY>
<jsp:useBean id="guess" class="tom.jiafei.GuessNumber" scope="session"/>
<jsp:setProperty name="guess" property="guessNumber" param="guessNumber"/>
<BR> <jsp:getProperty name="guess" property="result"/>这是第
<jsp:getProperty name="guess" property="guessCount"/>次猜
你给出的数是<jsp:getProperty name="guess" property="guessNumber"/>
<%if(guess.isRight()==false){%>
<FORM action="<%=strGuess%>" method=post>
再输入你的猜测:<input type=text name="guessNumber">
<input type=submit value="提交">
</FORM>
<% } %>
<BR><A href="<%=strGetNumber%>">链接到getnumber.jsp重新玩猜数</A>
</BODY></HTML>
检查了很多次找不出原因所以把三个代码都写上了 有点多 请会的前辈帮忙运行下 找出错误 谢啦 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询