-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
54 lines (48 loc) · 2.22 KB
/
GuessingGame.java
File metadata and controls
54 lines (48 loc) · 2.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
// package guessinggame;
import javax.swing.*;
public class GuessingGame {
public static void main(String[] args) {
// Create random number between 1-100 for CPU
int computerNumber = (int) (Math.random() * 10 + 1);
// Initialize user variable
int userAnswer = 0;
// Print the random CPU number to the console
// System.out.println("The correct guess would be: " + computerNumber);
// Initialize the first attempted guess count
int count = 1;
// Value to tell when game is over
boolean gameOver = false;
while (userAnswer != computerNumber && gameOver == false) {
// Display small dialog box. 3 = Question Mark icon
String response = JOptionPane.showInputDialog(null, "Enter a guess between 1 & 10", "Guessing Game", 3);
userAnswer = Integer.parseInt(response);
// Run determineGuess to get answer and display results
JOptionPane.showMessageDialog(null, "" + determineGuess(userAnswer, computerNumber, count));
count++;
if (count == 6) {
gameOver = true;
}
} // end while
// Game over screen
if (count == 6) {
JOptionPane.showMessageDialog(null,
"Game Over.\nYou have reached the maximum number of guess attempts.\n The correct number was: "
+ computerNumber,
"Game Over", 2);
}
} // end main
// Class to determine whethere or not the user is correct
public static String determineGuess(int userAnswer, int computerNumber, int count) {
if (userAnswer <= 0 || userAnswer > 10) {
return "Your guess is invalid";
} else if (userAnswer == computerNumber) {
return "Correct!\nTotal Guesses: " + count;
} else if (userAnswer > computerNumber) {
return "Your guess is too high, try again.\nAttempt Number: " + count;
} else if (userAnswer < computerNumber) {
return "Your guess is too low, try again.\n Attempt Number: " + count;
} else {
return "Your guess is incorrect.\nAttempt Number: " + count;
}
} // end class
} // end program