JSP learning notes (seven) ----- Guess digital game

xiaoxiao2021-03-06  88

1. This is a guess number game, by using JSP call Javabean

2. Need two files, number.jsp and NumberguessBean.java

3. First look at Number.jsp code:

<%

IF (Number.GetNumGuesses () == 0)

{

%>

Welcome to the Number Guest Game.


I'm thinking of a number between 1 and 100.


What's your guess?

<%

}

Else IF (Number.getsuccess ())

{

%>

CONGRATULATIONS! You got it. And after just <% = number.getnumguesses ()%> Tries.

Care to TRY AGAIN ?

<%

Number.reset ();

}

Else

{

%>

Good guess, but nope. Try <% = Number.getHint ()%>. You have name <% = number.getnumguesses ()%> Guesses.


I'm Thinking of a Number Between 1 and 100.


What's your guess?

<%

}

%>

4. NumberguessBean.java code is as follows:

Import java.util. *;

Public class numberguessbean {

Int Answer;

Boolean Success;

String hint;

INT NUMGUESS;

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;

}

ELSE IF (g == -1) {

Hint = "a Number next Time";

}

ELSE IF (g

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;

}

}

转载请注明原文地址:https://www.9cbs.com/read-122280.html

New Post(0)