0% found this document useful (0 votes)
43 views

Number Guessing Game Case Study

Uploaded by

Sakshi Deshmukh
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)
43 views

Number Guessing Game Case Study

Uploaded by

Sakshi Deshmukh
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
You are on page 1/ 2

Number Guessing Game Case Study

Introduction
This case study focuses on the development of a simple Number Guessing Game using Java.
The game challenges the player to guess a randomly generated number within a specific
range. The player is provided feedback on whether their guess is too high or too low, and
the game continues until the correct number is guessed. This project is designed to
demonstrate the use of basic Java concepts, including loops, conditionals, and random
number generation.

Basic Information
1. **Programming Language:** Java
2. **Libraries Used:** java.util.Random, java.util.Scanner
3. **Key Features:**
- Random number generation
- User input and feedback
- Loop until the correct guess is made

System Features
The Number Guessing Game provides the following features:
1. **Random Number Generation:** The system generates a random number between 1 and
100.
2. **User Input:** The player enters their guess using the console.
3. **Feedback Mechanism:** The system informs the player if the guess is too high, too low,
or correct.
4. **Loop Until Correct:** The game continues until the player guesses the correct number.

Code Implementation
Below is a sample Java code for the Number Guessing Game:

```java
import java.util.Random;
import java.util.Scanner;

public class NumberGuessingGame {


public static void main(String[] args) {
Random rand = new Random();
Scanner scanner = new Scanner(System.in);
int numberToGuess = rand.nextInt(100) + 1;
int numberOfTries = 0;
int guess = 0;
boolean hasGuessedCorrectly = false;

System.out.println("Welcome to the Number Guessing Game!");


while (!hasGuessedCorrectly) {
System.out.print("Enter your guess (1-100): ");
guess = scanner.nextInt();
numberOfTries++;

if (guess < numberToGuess) {


System.out.println("Too low!");
} else if (guess > numberToGuess) {
System.out.println("Too high!");
} else {
hasGuessedCorrectly = true;
System.out.println("Correct! You guessed the number in " + numberOfTries + "
tries.");
}
}
}
}
```

Sample Output
Here is an example of the output when playing the game:

```
Welcome to the Number Guessing Game!
Enter your guess (1-100): 50
Too low!
Enter your guess (1-100): 75
Too high!
Enter your guess (1-100): 63
Correct! You guessed the number in 3 tries.
```
This output shows the player's attempts to guess the correct number.

You might also like