Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Simple Number Guessing Game.
* The player guesses a random number between 1 and 100.
* Reference: https://en.wikipedia.org/wiki/Guess_the_number
*/

package com.thealgorithms.puzzlesandgames;
import java.util.Scanner;

public final class NumberGuess {
private NumberGuess() {
throw new AssertionError("Cannot instantiate NumberGuess");
}

public static void playGame() {
Scanner sc = new Scanner(System.in);
int number = (int) (Math.random() * 100) + 1;
int guess = 0;
int tries = 0;

System.out.println("🎯 Welcome to the Number Guessing Game!");
System.out.println("I've picked a number between 1 and 100. Try to guess it!\n");

while (true) {
System.out.print("Enter your guess: ");

if (!sc.hasNextInt()) {
System.out.println("Please enter a valid number.");
sc.next();
continue;
}

guess = sc.nextInt();
tries++;

if (guess < 1 || guess > 100) {
System.out.println("⚠️ Please guess a number between 1 and 100.");
continue;
}

if (guess < number) {
System.out.println("Too low! 📉");
} else if (guess > number) {
System.out.println("Too high! 📈");
} else {
System.out.println("🎉 Correct! The number was " + number + ".");
System.out.println("You took " + tries + " tries.");
break;
}
}

sc.close();
}
}