-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessGameApp.java
More file actions
69 lines (50 loc) · 1.95 KB
/
Copy pathGuessGameApp.java
File metadata and controls
69 lines (50 loc) · 1.95 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
package mini_apps;
/**
* User tries to guess a secret random generated number
* @author Giannis
* @version 1.0.0
*/
import java.util.Scanner;
public class GuessGameApp {
// initialization of to be guessed numbers and max retries
private static final int MAX_TRIES = 5;
private static boolean run = true;
// Start of game
public static void main(String[] args) {
int genNum,retries,inNum;
// Start of guessing game
Scanner reader = new Scanner(System.in);
while (run) {
genNum = (int) (Math.random() * 101);
retries = MAX_TRIES;
// Start of retries loop
while (retries > 0) {
System.out.print("Give an integer between 0 and 100 ( 0 and 100 included): ");
inNum = reader.nextInt();
if (inNum == genNum) {
System.out.println("You guessed correctly congrats!");
break; // Stops retires loop
}
retries--;
if (inNum > genNum) {
System.out.printf("Too high... You have: %d retries remains\n", retries);
} else {
System.out.printf("Too low... You have: %d retries remains\n", retries);
}
}
// checks if reties = 0 and asks for a rematch
if (retries == 0) {
System.out.println("Game lost.. the number was " + genNum);
}
System.out.print("Do you want a re-match?? (Yes / No): ");
String resp = reader.next();
if (resp.equalsIgnoreCase("Yes") || resp.equalsIgnoreCase("y") ) {
System.out.println("//...Game Restarting...//");
} else if (resp.equalsIgnoreCase("No") || resp.equalsIgnoreCase("n")) {
System.out.println("//...Game Ended...//");
break;
}
}
reader.close();
}
}