-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGameApp.java
More file actions
84 lines (72 loc) · 2.77 KB
/
GuessingGameApp.java
File metadata and controls
84 lines (72 loc) · 2.77 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class GuessingGameApp {
private int randomNumber;
private int attempts;
private JFrame frame;
private JLabel instructionsLabel;
private JTextField guessField;
private JLabel feedbackLabel;
private JLabel attemptsLabel;
private JButton submitButton;
public GuessingGameApp() {
frame = new JFrame("Guessing Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
instructionsLabel = new JLabel("Guess the number between 1 and 100:");
guessField = new JTextField(10);
feedbackLabel = new JLabel();
attemptsLabel = new JLabel("Attempts: 0");
submitButton = new JButton("Submit");
frame.add(instructionsLabel);
frame.add(guessField);
frame.add(feedbackLabel);
frame.add(attemptsLabel);
frame.add(submitButton);
// Generate a random number
Random rand = new Random();
randomNumber = rand.nextInt(100) + 1;
// Handle user input and game logic
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
frame.pack();
frame.setVisible(true);
}
private void checkGuess() {
try {
int guess = Integer.parseInt(guessField.getText());
attempts++;
if (guess == randomNumber) {
feedbackLabel.setText("Congratulations! You guessed the correct number.");
showResultsDialog(1, attempts); // 1 for correct guess
submitButton.setEnabled(false); // Disable further attempts
} else if (guess < randomNumber) {
feedbackLabel.setText("Too low. Try again.");
} else {
feedbackLabel.setText("Too high. Try again.");
}
attemptsLabel.setText("Attempts: " + attempts);
} catch (NumberFormatException e) {
feedbackLabel.setText("Please enter a valid number.");
}
}
private void showResultsDialog(int correctAttempts, int totalAttempts) {
double percentage = ((double) correctAttempts / totalAttempts) * 100;
String message = String.format("You guessed correctly %.2f%% of the time.", percentage);
JOptionPane.showMessageDialog(frame, message, "Game Results", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GuessingGameApp();
}
});
}
}