<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<jsp:useBean id="numguess" class="text.GuessBean" scope="session" />
<jsp:setProperty name="numguess" property="*" />
<%
if(numguess.getSuccess())
{
%>
恭喜你猜中了!
<br/>
你一共猜了<%=numguess.getNumGuesses() %>
<% numguess.reset(); %>
还想<a href="index.jsp" >继续猜</a>
?
<%
}else if(numguess.getNumGuesses()==0)
{
%>
欢迎来参加猜数字游戏:
<br/>
我想出了一共从1到100之间的数字:
<br/>
<from method=get >
你猜是多少 ?:<input type="text" name="guess"/>
<input type="submit" value="Submit" />
</from>
<% }else{
%>很好!可惜没对,试试<b><%=numguess.getHint() %></b>
你已经猜了<%=numguess.getNumGuesses() %>次
<br/>
<br/>
我想出了一共从1到100之间的数字:
<br/>
<from method=get >
你猜是多少 ?:<input type="text" name="guess"/>
<input type="submit" value="Submit"/>
</from>
<% } %>
</body>
</html>
我写的java类是
package text;
import java.util.*;
public class GuessBean {
int answer;
boolean success;
String hint;
int numGuesses;
public GuessBean(){
reset();
}
public void setGuess(String guess)
{
numGuesses++;
int g;
try{
g=Integer.parseInt(guess);
}
catch(NumberFormatException e){
g=-1;
}
if(g==answer){
success=true;
}else if (g==-1)
{
hint="a number next time";
}else if(g<answer)
{
hint="highter";
}else if(g>answer)
{
hint="lower";
}
}
public boolean getSuccess(){
return success;
}
public String getHint(){
return ""+hint;
}
public int getNumGuesses()
{
return numGuesses;
}
public void reset(){
answer=Math.abs(new Random().nextInt()%100)+1;
success=false;
numGuesses=0;
}
}
------解决方案--------------------
lz想问什么呀?