Java Reference
In-Depth Information
LISTING 5.5
//********************************************************************
// Guessing.java Author: Lewis/Loftus
//
// Demonstrates the use of a block statement in an if-else.
//********************************************************************
import
java.util.*;
public class
Guessing
{
//-----------------------------------------------------------------
// Plays a simple guessing game with the user.
//-----------------------------------------------------------------
public static void
main (String[] args)
{
final int
MAX = 10;
int
answer, guess;
Scanner scan =
new
Scanner (System.in);
Random generator =
new
Random();
answer = generator.nextInt(MAX) + 1;
System.out.print ("I'm thinking of a number between 1 and "
+ MAX + ". Guess what it is: ");
guess = scan.nextInt();
if
(guess == answer)
System.out.println ("You got it! Good guessing!");
else
{
System.out.println ("That is not correct, sorry.");
System.out.println ("The number was " + answer);
}
}
}
OUTPUT
I'm thinking of a number between 1 and 10. Guess what it is:
7
That is not correct, sorry.
The number was 5
Search WWH ::
Custom Search