Advertisement
grodek118

Random Number Guessing Game Enhancement

Dec 1st, 2022
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args){
  7.  
  8.         int randomNumber;
  9.         int guessNumber;
  10.         int sum = 0;
  11.  
  12.         Scanner scanner = new Scanner(System.in);
  13.         Random random = new Random();
  14.         randomNumber = random.nextInt(100);
  15.  
  16.         guessNumber = scanner.nextInt();
  17.  
  18.         while (guessNumber != randomNumber)
  19.         {
  20.             guessNumber = scanner.nextInt();
  21.             sum++;
  22.  
  23.             if (guessNumber < randomNumber)
  24.             {
  25.                 System.out.println("Too low, try again");
  26.             }
  27.             else if (guessNumber > randomNumber)
  28.             {
  29.                 System.out.println("Too high, try again");
  30.             }
  31.             else
  32.             {
  33.                 System.out.println("You guessed right");
  34.                 System.out.println(sum);
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement