-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch_43_number_game.java
43 lines (37 loc) · 1.01 KB
/
ch_43_number_game.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
36
37
38
39
40
41
42
43
import java.util.Random;
import java.util.Scanner;
class game {
int number;
int choice;
int attempt = 0;
Scanner sc = new Scanner(System.in);
Random r = new Random();
game() {
number = 1 + r.nextInt(100);
}
public void getInput() {
System.out.print("Enter a Number between 1 - 100 : ");
choice = sc.nextInt();
attempt++;
Check();
}
public void Check() {
if (choice == number) {
System.out.println("You found the Number : " + choice + " !!!");
System.out.println("No of attempts : " + attempt);
return;
} else if (choice > number) {
System.out.println("The Number is smaller than " + choice);
getInput();
} else {
System.out.println("The Number is greater than " + choice);
getInput();
}
}
}
public class ch_43_number_game {
public static void main(String[] args) {
game g = new game();
g.getInput();
}
}