-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessingGame.java
More file actions
35 lines (29 loc) · 1.05 KB
/
NumberGuessingGame.java
File metadata and controls
35 lines (29 loc) · 1.05 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
import java.util.Random;
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
// Number Guessing Game
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int guess;
int min = 1;
int max = 100;
int randomNumber = random.nextInt(min, max + 1);
int attempts = 0;
System.out.println("Number Guessing Game");
do {
System.out.printf("Guess a number between %d-%d: ", min, max);
guess = scanner.nextInt();
attempts++;
if(guess < randomNumber) {
System.out.println("Too low! Try Again.");
} else if (guess > randomNumber) {
System.out.println("Too high! Try Again.");
} else {
System.out.println("Correct! The number was " + randomNumber);
System.out.println("Number of Attempts: " + attempts);
}
} while(guess != randomNumber);
scanner.close();
}
}