0% found this document useful (0 votes)
4 views5 pages

Java Number Guessing Game Code

The document is a Java program for a Number Guessing Game, where players can choose difficulty levels and guess a randomly generated number within a specified range. It includes features like a main menu, instructions, a scoreboard to track total and highest scores, and the ability to play multiple rounds. The game utilizes a scanner for user input and handles invalid inputs gracefully.

Uploaded by

1032231050
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Java Number Guessing Game Code

The document is a Java program for a Number Guessing Game, where players can choose difficulty levels and guess a randomly generated number within a specified range. It includes features like a main menu, instructions, a scoreboard to track total and highest scores, and the ability to play multiple rounds. The game utilizes a scanner for user input and handles invalid inputs gracefully.

Uploaded by

1032231050
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

Random;

import [Link];

public class NumberGuessingGame {

public static int totalScore = 0;

public static int highestScore = 0;

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

boolean exit = false;

printWelcomeBanner();

while (!exit) {

showMainMenu();

int choice = getValidatedInteger(scanner, "Enter your choice: ");

switch (choice) {

case 1:

playGame(scanner);

break;

case 2:

showInstructions();

break;

case 3:

showScoreboard();

break;

case 4:

exit = true;

[Link]("\nThank you for playing!");

break;

default:

[Link]("Invalid choice! Please choose between 1


to 4.");
}

[Link]();

public static void printWelcomeBanner() {

[Link]("================================
==========");

[Link](" WELCOME TO NUMBER GUESSING GAME!


");

[Link]("================================
==========");

public static void showMainMenu() {

[Link]("\n--------- MAIN MENU ---------");

[Link]("1. Start Game");

[Link]("2. Instructions");

[Link]("3. Scoreboard");

[Link]("4. Exit");

[Link]("-----------------------------");

public static void showInstructions() {

[Link]("\n----- INSTRUCTIONS -----");

[Link]("[Link] a difficulty level.");

[Link]("[Link] the secret number.");

[Link]("[Link] depend on remaining attempts.");

[Link]("-------------------------\n");

public static void showScoreboard() {

[Link]("\n--------- SCOREBOARD ---------");


[Link]("Total Score : " + totalScore);

[Link]("Highest Score : " + highestScore);

[Link]("-------------------------------\n");

public static void playGame(Scanner scanner) {

[Link]("\nSelect Difficulty:");

[Link]("1. Easy (Between 1 to 50,5 attempts)");

[Link]("2. Medium (Between 1 to 100,7 attempts)");

[Link]("3. Hard (Between 1 to 500,9 attempts)");

[Link]("4. Expert (Between 1 to 1000,10 attempts)");

int difficulty = getValidatedInteger(scanner,"Choose difficulty:");

int lowerBound = 1;

int upperBound = 100;

int maxAttempts = 7;

switch (difficulty) {

case 1: upperBound = 50; maxAttempts = 5; break;

case 2: upperBound = 100; maxAttempts = 7; break;

case 3: upperBound = 500; maxAttempts = 9; break;

case 4: upperBound = 1000; maxAttempts = 10; break;

default:

[Link]("Invalid choice,using Medium mode.");

Random random = new Random();

int RandomNumber = [Link](upperBound - lowerBound + 1)


+ lowerBound;

[Link]("\nGuess the number between %d and %d.


Attempts: %d\n",

lowerBound, upperBound, maxAttempts);

int attemptsMade = 0;
boolean guessed = false;

while (attemptsMade < maxAttempts && !guessed) {

int guess = getValidatedInteger(scanner, "Enter your guess: ");

attemptsMade++;

if (guess == RandomNumber) {

[Link]("\n Correct! The number was %d!\n",


RandomNumber);

int roundScore = (maxAttempts - attemptsMade + 1) * 10;

totalScore += roundScore;

highestScore = [Link](highestScore, roundScore);

[Link]("Round Score: " + roundScore);

guessed = true;

else if (guess < RandomNumber) {

[Link]("Too low!");

else {

[Link]("Too high!");

if (!guessed) {

[Link]("\nOut of attempts! The Random number was "


+ RandomNumber);

[Link]("Total Score: " + totalScore);

[Link]();

[Link]("Do you want to play another round? (yes/no): ");

String again = [Link]().toLowerCase();

if ([Link]("yes")) {

playGame(scanner);
}

else if ([Link]("no")) {

[Link]("\nExiting the game... Goodbye!");

[Link](0); // <-- EXIT ENTIRE PROGRAM

else {

[Link]("Invalid choice! Exiting the game...");

[Link](0);

public static int getValidatedInteger(Scanner scanner, String message)


{

while (true) {

try {

[Link](message);

return [Link]();

} catch (Exception e) {

[Link]("Invalid input! Enter a number.");

[Link](); // clear invalid input

You might also like