Advertisement
Guest User

Untitled

a guest
Dec 12th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scanner = new Scanner(System.in);
  8.         boolean isPlaying = true;
  9.         String retry = "";
  10.         Random random = new Random();
  11.  
  12.         while (isPlaying) {
  13.  
  14.             System.out.println("***********************************");
  15.             System.out.println("<(<<Welcome To guess the number>>)>");
  16.             System.out.println("***********************************");
  17.  
  18.             System.out.print("\nPress enter to play...");
  19.             scanner.nextLine();
  20.             int number = random.nextInt(10) + 1;
  21.             System.out.print("Guess a number between 1 and 10: ");
  22.             int guess = scanner.nextInt();
  23.             scanner.nextLine();
  24.  
  25.             if (guess == number) {
  26.                 System.out.print("\nYou won! Do you want to play again?(Yes or No): ");
  27.             }
  28.  
  29.             else {
  30.                 System.out.print("\nYou lost! Do you want to play again?(Yes or No): ");
  31.  
  32.             }
  33.             retry = scanner.nextLine();
  34.  
  35.             while (!retry.equalsIgnoreCase("Yes") && !retry.equalsIgnoreCase("No")){
  36.                 System.out.print("\nInvalid! please type YES or NO: ");
  37.                 retry = scanner.nextLine();
  38.             }
  39.             isPlaying = retry.equalsIgnoreCase("Yes");
  40.         }
  41.  
  42.         scanner.close();
  43.         System.out.println("Thanks for playing!");
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement