-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_50_exercise_solution.java
70 lines (57 loc) · 1.18 KB
/
cwh_50_exercise_solution.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
69
70
package company;
import java.util.Random;
import java.util.Scanner;
class Game
{
int number;
int inputNumber;
int noOfGuesses = 0;
Scanner sc = new Scanner(System.in);
public int getNoOfGuesses() {
return noOfGuesses;
}
public void setNoOfGuesses(int noOfGuesses) {
this.noOfGuesses = noOfGuesses;
}
Game()
{
Random rand = new Random();
this.number = rand.nextInt(100);
}
void takeUserInput()
{
System.out.print("\nGuess the number : ");
inputNumber = sc.nextInt();
}
boolean isCorrectNumber()
{
noOfGuesses++;
if(inputNumber == number)
{
System.out.format("\nYes you guessed it right. \nThe number is %d, and you guessed it in %d attempts." ,number,noOfGuesses);
return true;
}
else if(inputNumber < number)
{
System.out.println("You gussed it low.");
}
else if(inputNumber > number)
{
System.out.println("you guessed it high.");
}
return false;
}
}
public class cwh_50_exercise_solution
{
public static void main(String[] args)
{
Game g = new Game();
boolean b = false;
while(!b)
{
g.takeUserInput();
b = g.isCorrectNumber();
}
}
}