-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guess The Number Game
75 lines (68 loc) · 1.4 KB
/
Guess The Number Game
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
71
72
73
74
75
import java.util.Random;
import java.util.Scanner;
class game {
public int number,inputNumber,noofguesses;
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.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 Exercise3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
game g = new game();
boolean b = false;
while(!b) {
g.takeuserinput();
b=g.isCorrectNumber();
}
}
}
/*OUTPUT:
* Guess the number
23
Too low...
Guess the number
45
Too high...
Guess the number
40
Too high...
Guess the number
35
Too high...
Guess the number
30
Too high...
Guess the number
25
Too low...
Guess the number
27
Yes you guessed it right, it was 27
You guessed it in 7 attempts */