forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessTheNumber.java
68 lines (58 loc) · 1.71 KB
/
GuessTheNumber.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.Scanner;
class Game {
private int score = 0, random;
Game() {
System.out.println(
"Game Rules : There is a secret number between 1-100 and you have to guess, you got unlimited attempts.\nThe lesser atempt you take the better you are ... Have Fun !!\n");
random = (int) (Math.random() * 100);
}
public void getScore() {
System.out.println("Hurray !!! You guessed the secret number in " + score + " attempt");
}
public void setScore() {
score++;
}
public int getUserInput() {
System.out.print("Enter a Number :");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
return input;
}
public boolean isCorrect(int num) {
if (num == random) {
return true;
}
return false;
}
public boolean isGreater(int num) {
if (num > random) {
return true;
}
return false;
}
public boolean isSmaller(int num) {
if (num < random) {
return true;
}
return false;
}
}
public class GuessTheNumber {
public static void main(String[] args) {
int num;
Game obj = new Game();
while (true) {
num = obj.getUserInput();
if (obj.isCorrect(num)) {
obj.setScore();
obj.getScore();
break;
} else if (obj.isGreater(num)) {
System.out.println("The secret number is less than this ...");
} else if (obj.isSmaller(num)) {
System.out.println("The secret number is greater than this ...");
}
obj.setScore();
}
}
}