- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_3.java
More file actions
Latest commit
62 lines (55 loc) · 1.72 KB
/
Copy pathExercise_3.java
File metadata and controls
62 lines (55 loc) · 1.72 KB
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
importjava.util.Random;
importjava.util.Scanner;
classGame{
publicintnumber;
publicintinputNumber;
publicintnoOfGuesses = 0;
publicintgetNoOfGuesses() {
returnnoOfGuesses;
}
publicvoidsetNoOfGuesses(intnoOfGuesses) {
this.noOfGuesses = noOfGuesses;
}
Game(){
Randomrand = newRandom();
this.number = rand.nextInt(100);
}
voidtakeUserInput(){
System.out.println("Guess the number");
Scannersc = newScanner(System.in);
inputNumber = sc.nextInt();
}
booleanisCorrectNumber(){
noOfGuesses++;
if (inputNumber==number){
System.out.format("Yes you guessed it right, it was %d\nYou guessed it in %d attempts", number, noOfGuesses);
returntrue;
}
elseif(inputNumber<number){
System.out.println("Too low...");
}
elseif(inputNumber>number){
System.out.println("Too high...");
}
returnfalse;
}
}
publicclassExercise_3 {
publicstaticvoidmain(String[] args) {
/*
Create a class Game, which allows a user to play "Guess the Number"
game once. Game should have the following methods:
1. Constructor to generate the random number
2. takeUserInput() to take a user input of number
3. isCorrectNumber() to detect whether the number entered by the user is true
4. getter and setter for noOfGuesses
Use properties such as noOfGuesses(int), etc to get this task done!
*/
Gameg = newGame();
booleanb= false;
while(!b){
g.takeUserInput();
b = g.isCorrectNumber();
}
}
}