-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessGame2.java
More file actions
54 lines (48 loc) · 1.5 KB
/
guessGame2.java
File metadata and controls
54 lines (48 loc) · 1.5 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
import java.util.*;
public class guessGame2 {
static Scanner sc = new Scanner(System.in);
public static void main(String[]args) {
do {
playGame();
} while (keepPlaying());
System.out.println("...Thank You for Playing...");
}
public static void playGame() {
int number,guess;
// guess the number;
guess = guessInput();
System.out.println("I guess the number between 1 and 10 ... \n now your turn" + guess);
number = getGuess();
if (guess == number) {
System.out.println("wow You are Right!!!");
} else {
System.out.println("Oh no sry..");
}
}
public static int guessInput() {
return (int)(Math.random()*10) + 1;
}
public static int getGuess() {
while (true) {
int number = sc.nextInt();
if (number < 0 || number > 10) {
System.out.println("try again please enter number between 1 to 10");
} else {
return number;
}
}
}
public static boolean keepPlaying() {
// play again
while (true) {
System.out.println("want to repeat a game again ... press Y, if no press N");
String repeatOrNot = sc.next();
if (repeatOrNot.equalsIgnoreCase("Y")) {
return true;
}
else if (repeatOrNot.equalsIgnoreCase("N")) {
return false;
}
}
}
}