<% } else { %> Good guess, but nope. Try <b><%= numguess.getHint() %></b>. You have made <%= numguess.getNumGuesses() %> guesses. <p>I'm thinking of a number between 1 and 100. <p><form method=get>
操作程序(NumberGuessBean.java) // Number Guess Game // Written by Jason Hunter, CTO, K&A Software // jasonh@kasoftware.com, http://www.servlets.com // Copyright 1999, K&A Software // Distributed by Sun Microsystems with permission
package num; import java.util.*; public class NumberGuessBean { int answer; boolean success; String hint; int numGuesses; public NumberGuessBean() { 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 = "higher"; } 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; } }