-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessingGame.java
35 lines (30 loc) · 1.26 KB
/
GuessingGame.java
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
import java.util.Scanner;
import java.util.Random;
public class GuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int numberToGuess = random.nextInt(100) + 1;
int userGuess = 0;
int attempts = 0;
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("I have chosen a number between 1 and 100. Try to guess it!");
while (userGuess != numberToGuess) {
System.out.print("Enter your guess: ");
try {
userGuess = Integer.parseInt(scanner.nextLine());
attempts++;
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 it in " + attempts + " attempts.");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
}
scanner.close();
}
}