//Flag-controlled while loop.
//Guessing the number game.
//Filename: FlagControlledLoop
import java.util.*;
public class FlagControlledLoop
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//declare the variables
int num; //variable to store the random number
int guess; //variable to store the number
//guessed by the user
boolean done; //boolean variable to control the loop
num = (int) (Math.random() * 100); //Line 1
done = false; //Line 2
while (!done) //Line 3
{ //Line 4
System.out.print ("Enter an integer greater" + " than or equal to 0 and " + "less than 100: "); //Line 5
guess = console.nextInt(); //Line 6
System.out.println(); //Line 7
if (guess == num) //Line 8
{ //Line 9
System.out.println("You guessed the " + "correct number."); //Line 10
done = true; //Line 11
} //Line 12
else if (guess < num) //Line 13
System.out.println("Your guess is " + "lower than " + "the number.\n" + "Guess again!"); //Line 14
else //Line 15
System.out.println("Your guess is " + "higher than " + "the number.\n" + "Guess again!"); //Line 16
} //end while //Line 17
} //Line 18
}
Sample Output:
******************************************************************
Enter an integer greater than or equal to 0 and less than 100: 25
Your guess is higher than the number.
Guess again!
Enter an integer greater than or equal to 0 and less than 100: 5
Your guess is lower than the number.
Guess again!
Enter an integer greater than or equal to 0 and less than 100: 10
Your guess is higher than the number.
Guess again!
Enter an integer greater than or equal to 0 and less than 100: 8
Your guess is higher than the number.
Guess again!
Enter an integer greater than or equal to 0 and less than 100: 6
Your guess is lower than the number.
Guess again!
Enter an integer greater than or equal to 0 and less than 100: 7
You guessed the correct number.
The preceding program works as follows: The statement in Line 1 creates an integer
greater than or equal to 0 and less than 100 and stores this number in the variable num.
The statement in Line 2 sets the boolean variable done to false. The while loop starts
at Line 3 and ends at Line 17. The expression in the while loop at Line 3 evaluates the
expression !done. If done is false, then !done is true and the body of the while loop
executes; if done is true, then !done is false, so the while loop terminates.
The statement in Line 5 prompts the user to enter an integer greater than or equal to 0
and less than 100. The statement in Line 6 stores the number entered by the user in the
variable guess. The expression in the if statement in Line 8 determines whether
the value of guess is the same as num , that is, if the user guessed the number correctly.
If the value of guess is the same as num, then the statements in Lines 10 and 11 execute.
The statement in Line 10 outputs the message:
You guessed the correct number.
The statement in Line 11 sets the variable done to true. The control then goes back to
Line 3. Because done is true, !done is false and the while loop terminates.
If the expression in Line 8 evaluates to false, then the else statement in Line 13
executes. The statement part of this else is an if. . .else statement, starting at Line 13
and ending at Line 16. The if statement in Line 13 determines whether the value of
guess is less than num. In this case, the statement in Line 14 outputs the message:
Your guess is lower than the number.
Guess again!
If the expression in the if statement in Line 13 evaluates to false, then the statement in
Line 16 executes, which outputs the message:
Your guess is higher than the number.
Guess again!
The program then prompts the user to enter an integer greater than or equal to 0 and less
than 100.
No comments:
Post a Comment