fork download
  1. import java.io.InputStreamReader;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.util.Random;
  5.  
  6. class GuessTheNumber {
  7. public static void main (String[] args)
  8. throws IOException
  9. {
  10. int userValue = 0,
  11. randomNumber = new Random().nextInt(9)+1;
  12.  
  13. System.out.println("Guess a number between 1 and 10.");
  14.  
  15. do {
  16. System.out.println("Enter a number in the range of 1 to 10.");
  17. userValue = Integer.parseInt(in.readLine());
  18.  
  19. System.out.println("Guess: "+ userValue +". ");
  20.  
  21. if (userValue > randomNumber) {
  22. System.out.println("Your guess is too high.");
  23. }
  24. else if (userValue < randomNumber) {
  25. System.out.println("Your guess is too low.");
  26. }
  27.  
  28. System.out.println();
  29.  
  30. } while (randomNumber != userValue);
  31.  
  32. System.out.println("Congratulations! You guessed the number correctly!");
  33.  
  34. in.close();
  35. }
  36. }
Success #stdin #stdout 0.06s 380224KB
stdin
1
10
2
9
3
8
4
7
6
5
stdout
Guess a number between 1 and 10.
Enter a number in the range of 1 to 10.
Guess: 1. 
Your guess is too low.

Enter a number in the range of 1 to 10.
Guess: 10. 
Your guess is too high.

Enter a number in the range of 1 to 10.
Guess: 2. 
Your guess is too low.

Enter a number in the range of 1 to 10.
Guess: 9. 
Your guess is too high.

Enter a number in the range of 1 to 10.
Guess: 3. 
Your guess is too low.

Enter a number in the range of 1 to 10.
Guess: 8. 
Your guess is too high.

Enter a number in the range of 1 to 10.
Guess: 4. 

Congratulations! You guessed the number correctly!