-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessTheNumber.java
More file actions
169 lines (137 loc) · 4.22 KB
/
Copy pathGuessTheNumber.java
File metadata and controls
169 lines (137 loc) · 4.22 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.Scanner;
import java.util.Random;
class startGame
{
int InputFromSystem;
int InputFromUser;
int NoOfGuesses;
startGame()
{
Random r=new Random();
this.InputFromSystem=r.nextInt(100)+1;
}
public boolean takeUserInput()
{
if(NoOfGuesses<9)
{
System.out.print("Enter your guessed number : ");
this.InputFromUser = GuessTheNumber.takeIntegerInput(100);
NoOfGuesses++;
return false;
}
else
{
System.out.println("your attempts are over.... Better Luck Next Time ! \n");
return true;
}
}
public boolean isCorrectGuess()
{
if(InputFromSystem == InputFromUser)
{
System.out.println("Congrats! You guess the system generated number "+InputFromSystem+" in "+NoOfGuesses+" guesses");
switch (NoOfGuesses)
{
case 1:
System.out.println("your score is 100 out of 100 ");
System.out.println("you got 10 points out 10 points");
break;
case 2:
System.out.println("your score is 90 out of 100 ");
System.out.println("you got 9 points out 10 points");
break;
case 3:
System.out.println("your score is 80 out of 100 ");
System.out.println("you got 8 points out 10 points");
break;
case 4:
System.out.println("your score is 70 out of 100 ");
System.out.println("you got 7 points out 10 points");
break;
case 5:
System.out.println("your score is 60 out of 100 ");
System.out.println("you got 6 points out 10 points");
break;
case 6:
System.out.println("your score is 50 out of 100 ");
System.out.println("you got 5 points out 10 points");
break;
case 7:
System.out.println("your score is 40 out of 100 ");
System.out.println("you got 4 points out 10 points");
break;
case 8:
System.out.println("your score and points are too low. Better Luck Next Time ! ");
break;
}
System.out.println();
return true;
}
else if(NoOfGuesses<9 && InputFromSystem > InputFromUser)
{
System.out.println("System generated number is greater than "+InputFromUser);
}
else
System.out.println("System generated number is less than "+InputFromUser);
return false;
}
}
public class GuessTheNumber
{
public static int takeIntegerInput(int limit)
{
int input=0;
boolean flag=false;
Scanner s = new Scanner(System.in);
while(!flag)
{
try
{
input=s.nextInt();
flag=true;
if(flag && input >limit || input<1)
{
System.out.println("choose the number between 1 and "+limit);
flag=false;
}
}
catch(Exception e)
{
System.out.println("Enter only integer value ");
flag=false;
}
}
return input;
}
public static void main(String[] args)
{
int nextRound=1,noOfRounds=0;
System.out.println("1. Start the game \n2.Exit");
System.out.println("Enter your choice : ");
int choice = takeIntegerInput(2);
if(choice==1)
{
while(nextRound==1)
{
startGame stargame = new startGame();
boolean isMatched = false;
boolean isLimitCross = false;
System.out.println("\nRound "+noOfRounds+" starts......");
while(!isMatched && !isLimitCross)
{
isLimitCross = stargame.takeUserInput();
isMatched = stargame.isCorrectGuess();
}
System.out.println("1.Next Round \n2.Exit");
System.out.println("Enter your choice : ");
nextRound = takeIntegerInput(2);
if(nextRound !=1)
{
System.exit(0);
}
}
}
else
System.exit(0);
}
}