-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.java
55 lines (45 loc) · 1.29 KB
/
Game1.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
import java.util.Scanner;
import java.util.Random;
class Game {
public int number;
public int inputNo;
public int noOfGuesses = 0;
public int getNoOfGuesses() {
return noOfGuesses;
}
public void setNoOfGuesses(int noOfGuesses) {
noOfGuesses = noOfGuesses;
}
Game() {
Random ran = new Random();
this.number = ran.nextInt(100);
}
void takeUserInput() {
System.out.println("Guess the number");
Scanner sc = new Scanner(System.in);
inputNo = sc.nextInt();
}
boolean isCorrectNumber() {
noOfGuesses++;
if (inputNo == number) {
System.out.format("Yes, you guessed it right, it was %d\nYou guessed it %d attempts ", number,
noOfGuesses);
return true;
} else if (inputNo < number) {
System.out.println("Too low...");
} else if (inputNo > number) {
System.out.println("Too high...");
}
return false;
}
}
public class Game1 {
public static void main(String[] args) {
Game g = new Game();
boolean b = false;
while (!b) {
g.takeUserInput();
b = g.isCorrectNumber();
}
}
}