Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class GuessingGame {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Random random = new Random();
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. int number = random.nextInt(100)+1;
  11. int guess = -1;
  12. int count=0;
  13.  
  14. while (guess!=number) {
  15. count=count+1;
  16. System.out.print("Enter a number between 1 and 100: ");
  17. guess = scanner.nextInt();
  18. if (guess<number) {
  19. System.out.println("Too low, please try again");
  20. } else if (guess>number) {
  21. System.out.println("Too high, please try again");
  22. } else {
  23. System.out.println("Correct, the number was " + number);
  24. }
  25. }
  26. System.out.println("It took you " + count + " tries.");
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement