-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessthenumber.java
53 lines (46 loc) · 1.26 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
import java.util.Random;
import java.util.Scanner;
class Game{
public int number;
public int inputNumber;
public int noOfGuesses = 0;
public void setNoOfGuesses(int noOfGuesses) {
this.noOfGuesses = noOfGuesses;
}
public int getNoOfGuesses() {
return noOfGuesses;
}
public Game(){
Random rand = new Random();
this.number = rand.nextInt(100);
}
public void takeUserInput(){
System.out.println("Guess The Number: ");
Scanner sc = new Scanner(System.in);
inputNumber = sc.nextInt();
}
boolean isCorrectNumber(){
noOfGuesses++;
if (inputNumber==number){
System.out.format("Yes, You Guessed It Right, It Was %d\nYou Guessed It In %d Attempts", number, noOfGuesses);
return true;
}
else if(inputNumber<number){
System.out.println("Too Low...");
}
else if(inputNumber>number){
System.out.println("Too High...");
}
return false;
}
}
public class guessthenumber {
public static void main(String[] args) {
Game g = new Game();
boolean b = false;
while (!b) {
g.takeUserInput();
b = g.isCorrectNumber();
}
}
}