-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessgame.java
50 lines (44 loc) · 1.29 KB
/
guessgame.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
import java.util.*;
class OP {
private int noOfGuess;
private int compInput;
private int userInput;
Random r = new Random();
public OP() {
this.noOfGuess = 0;
this.compInput = r.nextInt(100);
}
public void takeInput() {
System.out.println("Guess number : ");
Scanner sc = new Scanner(System.in);
this.userInput = sc.nextInt();
this.isCorrect();
sc.close();
}
public void isCorrect() {
if (this.userInput == this.compInput) {
System.out.println("Your guess is correct!!");
System.out.println("The number was " + this.compInput);
this.noOfGuess++;
return;
} else if (this.compInput < this.userInput) {
System.out.println("The guessed number is too high !!");
this.noOfGuess++;
this.takeInput();
} else {
System.out.println("The guessed number is too low !!");
this.noOfGuess++;
this.takeInput();
}
}
public void getGuess() {
System.out.println("You took " + this.noOfGuess + " guesses!!");
}
}
public class guessgame {
public static void main(String[] args) {
OP task1 = new OP();
task1.takeInput();
task1.getGuess();
}
}