-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guessing.java
63 lines (56 loc) · 1.82 KB
/
Guessing.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
import java.util.Scanner;
class Guessing {
public static boolean mainGame() {
int guess = (int) (Math.random() * 10) + 1;
int numGuesses = 0;
boolean hit = false;
Scanner sc = new Scanner(System.in);
while(!hit) {
if(numGuesses == 5) {
System.out.println("Maximum number of guesses reached.");
break;
}
else {
System.out.println("Guess no: " + (numGuesses + 1));
System.out.print("Please enter an integer from 1-10: ");
int userGuess = sc.nextInt();
if(userGuess > guess) {
System.out.println("Guess too high!");
numGuesses++;
}
else if(userGuess < guess) {
System.out.println("Guess too low!");
numGuesses++;
}
else {
System.out.println("Jackpot!");
numGuesses++;
hit = true;
}
}
}
sc.close();
return hit;
}
public static void main(String[] args) {
// need to make the initial game
mainGame();
while(true) {
Scanner mainScan = new Scanner(System.in);
int newGame = 0;
System.out.print("Do you want to play again? (1 for yes, 0 for no): ");
newGame = mainScan.nextInt();
mainScan.close();
if(newGame == 0) {
System.out.println("Thank you for playing!");
break;
}
else if(newGame == 1) {
mainGame();
}
else {
System.out.println("Invalid input. Please enter another number.");
}
}
}
}