Java Reference
In-Depth Information
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!");
5
//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 Runs: (In the following sample run, the user input is shaded.)
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
greaterthanorequalto
0
and less than
100
and stores this number in the variable
num
.
Search WWH ::
Custom Search