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