From 8f59ade25691c496938e2fbff2da39032ef0ac9d Mon Sep 17 00:00:00 2001 From: Muhammad Waleed Ahmad <115546135+MuhammadWaleed-0786@users.noreply.github.com> Date: Sun, 15 Oct 2023 01:13:43 +0500 Subject: [PATCH] Create GuessingGame.java Create GuessingGame.java --- GuessingGame.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 GuessingGame.java diff --git a/GuessingGame.java b/GuessingGame.java new file mode 100644 index 00000000..b2eb409e --- /dev/null +++ b/GuessingGame.java @@ -0,0 +1,24 @@ +import java.util.Random; +import java.util.Scanner; + +public class GuessingGame { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Random random = new Random(); + int numberToGuess = random.nextInt(10) + 1; // Guess a number between 1 and 10 + int userGuess; + + do { + System.out.print("Guess the number (1-10): "); + userGuess = scanner.nextInt(); + + if (userGuess < numberToGuess) { + System.out.println("Too low. Try again."); + } else if (userGuess > numberToGuess) { + System.out.println("Too high. Try again."); + } else { + System.out.println("Congratulations! You guessed the correct number."); + } + } while (userGuess != numberToGuess); + } +}