-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessTheNumber.java
82 lines (79 loc) · 2.11 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Guess the number Game.
import java.util.Random;
import java.util.Scanner;
class Game{
Random ran = new Random();
Scanner sc = new Scanner(System.in);
int input,x;
private int noOfGuesses,chances;
String tries;
void setnoOfGuesses(){
noOfGuesses =1;
}
int getnoOfGuesses(){
return noOfGuesses;
}
void setnoOfchances(){
chances = 1;
}
int getnoOfChances(){
return chances;
}
Game(){
x = ran.nextInt(100);
}
int takeuserinput(){
System.out.println("Enter the number : ");
input = sc.nextInt();
return input;
}
void iscorrectnumber(){
if (input>x){
System.out.println("Number is larger");
gameon();
}
else{
System.out.println("Number is smaller");
gameon();
}
}
void gameon(){
takeuserinput();
System.out.println("Your number : "+ input);
if(input==x){
System.out.println("you won HOORAY!!!!!!");
System.out.println("Your number of Guesses are :" + getnoOfGuesses());
chance();
}
else{
noOfGuesses++;
iscorrectnumber();
}
}
void chance(){
System.out.println("Want to play one more Chance... If yes type \"yes\"... If no type \"no\"");
tries = sc.next();
switch (tries) {
case "yes" -> {
x = ran.nextInt(100);
chances++;
gameon();
}
case "no" -> {
System.out.println(".....Game is over.....");
}
}
}
}
public class guessTheNumber {
public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
Game game = new Game();
System.out.println(".....GUESS THE NUMBER.....");
//System.out.println(game.x);
game.setnoOfGuesses();
game.setnoOfchances();
game.gameon();
System.out.println("Your Total number of chances are : "+game.getnoOfChances());
}
}