-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame2.java
More file actions
56 lines (44 loc) · 1.13 KB
/
Game2.java
File metadata and controls
56 lines (44 loc) · 1.13 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
package sample;
import org.w3c.dom.ls.LSOutput;
import java.util.Scanner;
import java.util.Random;
class Game{
int number;
int userinput;
int noofguesses=0;
Game(){
Random rd = new Random();
this.number = rd.nextInt(100);
}
public void takeuserinput(){
System.out.println("Enter your input");
Scanner sc= new Scanner(System.in);
userinput=sc.nextInt();
}
boolean isCorrectNumber(){
noofguesses++;
if(number==userinput){
System.out.printf("You guessed it in %d attempts\n",noofguesses);
System.out.println("You guessed the number correct!!!!!");
return true;
}
else if(number<userinput){
System.out.println(" Entered number is Higher");
}
else if(number>userinput){
System.out.println("Entered number is Lower");
}
return false;
}
}
public class Game2 {
public static void main(String[] args) {
Game g = new Game();
g.takeuserinput();
boolean b = g.isCorrectNumber();
while(b!=true){
g.takeuserinput();
b=g.isCorrectNumber();
}
}
}