-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessNum.java
More file actions
27 lines (25 loc) · 894 Bytes
/
Copy pathGuessNum.java
File metadata and controls
27 lines (25 loc) · 894 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.Scanner;
import java.util.Random;
public class GuessNum{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random num = new Random();
int NumberToGuess = num.nextInt(100)+1;
int guess, attempts = 0;
System.out.println("Guess the number between 1 and 100");
while (true) {
System.out.println("Enter Your Guess Number: ");
guess = sc.nextInt();
attempts++;
if (guess == NumberToGuess) {
System.out.println("Correct! you guess it in "+ attempts + "attempts");
break;
} else if(guess>NumberToGuess){
System.out.println("Too High!");
} else{
System.out.println("Too low!");
}
}
sc.close();
}
}