-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathep6_game.java
55 lines (53 loc) · 1.22 KB
/
ep6_game.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
import java.util.Scanner;
import java.util.Random;
class Game{
public int n;
public int ui;
private int guess;
public int think;
public void setGuess(int guess){
this.guess = guess;
}
public int getGuess(){
return guess;
}
Game(){
Random rand = new Random();
n = rand.nextInt(100);
}
void takeinput(){
Scanner sc = new Scanner(System.in);
System.out.print("Guess the number (Between 1 to 100)= ");
ui = sc.nextInt();
}
boolean check(){
think++;
if(ui == n){
System.out.println("You guess it right, it was "+n+".\nYou guess it in "+think+" attempts.");
return true;
}
else if(ui < n){
System.out.println("Too low...");
}
else if(ui > n){
System.out.println("Too high...");
}
return false;
}
}
public class ep6_game {
public static void main(String[] args) {
Game g = new Game();
boolean b = false;
while(!b){
g.takeinput();
b = g.check();
if(b == true){
break;
}
else{
System.out.println("Try Again :)");
}
}
}
}