Java Bean + JSP guess game code

Source: Internet
Author: User
The game code is divided into two parts: one is the JavaBean code that generates random numbers and processes the guess information, and the other is the JSP display game page .!
 
// The following is a Java code used to guess a digital game to process and generate random numbers.
Package game;
Import java. util .*;
Public class guess
{
// Define the variable for storing the correct answer
Int answer;
// Defines whether to guess the correct variable
Boolean issuccess;
// Defines the variables for the judgment result.
String hint;
// Defines the variable of the number of player guesses
Int guess_num;

// Initialization
Public guess ()
{
Reset ();
}
// Checks whether the player input is correct and displays the corresponding prompt information.
Public void setguess (string guess)
{
This. guess_num ++;
Int I;
Try
{
I = integer. parseint (guess );
} Catch (numberformatexception E)
{
I =-1;
}
If (I = This. Answer)
{
This. issuccess = true;
}
Else if (I =-1)
{
This. Hint = "the format of the information you entered is incorrect. Please enter a number ";
}
Else if (I <this. Answer)
{
This. Hint = "the number you entered is smaller than the answer ";
}
Else if (I> This. Answer)
{
This. Hint = "the number you entered is greater than the answer ";
}
}
// Return the issuccess value.
Public Boolean getissuccess ()
{
Return this. issuccess;
}
// Return the hint value of the property.
Public String gethint ()
{
Return this. Hint;
}
// Return the value of the guess_num attribute.
Public int getguess_num ()
{
Return this. guess_num;
}
// Reset the game to indicate the start of the next new game
Public void reset ()
{
This. Answer = math. Abs (new random (). nextint () % 100) + 1;
Issuccess = false;
Guess_num = 0;
}
}
 
// The following figure shows the JSP code of the Operation page.
<% @ Page C Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<HTML xmlns = "http://www.w3.org/1999/xhtml ">
<Head>
<Meta http-equiv = "Content-Type" C/>
<JSP: usebean id = "Guess" Scope = "session" class = "game. Guess"/>
<Title> guess digital games </title>
</Head>
<Body>
<P>
<%
// Determine whether the Player starts playing
If (guess. getguess_num () = 0)
{
%>
<H1>
Welcome
</H>
</P>
<P> the number ranges from 1 to 100. </P>
<Form ID = "form1" name = "guess_form" method = "Post" Action = "guess. jsp">
Enter the number you guessed: & nbsp;
<Label>
<Input type = "text" name = "guess_num"/>
</Label>
& Nbsp;
<Label>
<Input type = "Submit" name = "Submit" value = "Guess this number"/>
</Label>
</Form>
<P> & nbsp; </P>
<%
Guess. setguess ("-1 ");
}
Else
{
String STR = request. getparameter ("guess_num"); // gets the number that the player guesses.
Guess. setguess (STR );
// Determine whether the player is guessing
If (guess. getissuccess ())
{
%>
<H1> <font color = "# ff0000"> congratulations! You guessed it </font>
<P> you have tried <font color = "# ff0000"> <% = guess. getguess_num ()-1%> </font> Times </P>
<%
Guess. Reset (); // reset game parameters
%>
<P>
<A href = "guess. jsp" target = "_ Self"> redo it! </A>
</P>
<P>
<%
}
Else
{
%>
</P>
<P> sorry! ! ! An error occurred !!! </P>
<P> <% = guess. gethint () %> </P>
<P> the value ranges from 1 to 100. </P>
<P> <br>
</P>
<Form ID = "form1" name = "guess_form" method = "Post" Action = "guess. jsp">
Enter the number you guessed: & nbsp;
<Label>
<Input type = "text" name = "guess_num"/>
</Label>
& Nbsp;
<Label>
<Input type = "Submit" name = "Submit" value = "Guess this number"/>
</Label>
</Form>
<%
}
}
%>
</Body>
</Html>
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.