-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessingGAME1.java
More file actions
63 lines (47 loc) · 1.23 KB
/
NumberGuessingGAME1.java
File metadata and controls
63 lines (47 loc) · 1.23 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
63
import java.util.Random;
import java.util.Scanner;
class GAME1{
public int number;
public int inputnumber;
public int noOfguesses=0;
public int getnoOfguesses() {
return noOfguesses;
}
public void setnoOfguesses(int noOfguesses) {
this.noOfguesses=noOfguesses;
}
GAME1() {
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\n u 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 NumberGuessingGAME1{
public static void main(String[] args) {
GAME1 g=new GAME1();
boolean b=false;
while(!b){
g.takeUserInput();
b=g.isCorrectNumber();
System.out.println(b);
}
}
}