-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessGame.java
More file actions
27 lines (24 loc) · 918 Bytes
/
NumberGuessGame.java
File metadata and controls
27 lines (24 loc) · 918 Bytes
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
import java.util.*;
public class NumberGuessGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int number = rand.nextInt(100) ;
int guess = 0, attempts = 0;
System.out.println(" Welcome to Number Guessing Game!");
System.out.println("I have chosen a number between 1 and 100. Try to guess it!");
while (guess != number) {
System.out.print("Enter your guess: ");
guess = sc.nextInt();
attempts++;
if (guess < number) {
System.out.println("Too low! Try again.");
} else if (guess > number) {
System.out.println("Too high! Try again.");
} else {
System.out.println(" Correct! You guessed it in " + attempts + " attempts.");
}
}
sc.close();
}
}