forked from CSEdgeOfficial/Java-Programming-Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessingGame.java
More file actions
41 lines (34 loc) · 1.44 KB
/
NumberGuessingGame.java
File metadata and controls
41 lines (34 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Random;
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int minNumber = 1;
int maxNumber = 100;
int targetNumber = random.nextInt(maxNumber - minNumber + 1) + minNumber;
int maxAttempts = 5;
int attempts = 0;
boolean guessedCorrectly = false;
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("I've picked a number between " + minNumber + " and " + maxNumber + ". Can you guess it?");
while (attempts < maxAttempts) {
System.out.print("Enter your guess: ");
int guess = scanner.nextInt();
attempts++;
if (guess == targetNumber) {
System.out.println("Congratulations! You've guessed the correct number in " + attempts + " attempts!");
guessedCorrectly = true;
break;
} else if (guess < targetNumber) {
System.out.println("Too low! Try again.");
} else {
System.out.println("Too high! Try again.");
}
}
if (!guessedCorrectly) {
System.out.println("Sorry, you've used all your attempts. The correct number was: " + targetNumber);
}
scanner.close();
}
}