import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class ArithmeticQuizGUI extends JFrame {
private static final int NUMBER_OF_QUESTIONS = 10;
private static final int TIME_LIMIT_PER_QUESTION = 30; // seconds
private List
private int currentQuestionIndex = 0;
private int correctAnswers = 0;
private int wrongAnswers = 0;
private JLabel questionLabel;
private JTextField answerField;
private JLabel timerLabel;
private Timer timer;
private int remainingTime;
public ArithmeticQuizGUI() {
setTitle("Arithmetic Quiz");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Generate questions
generateQuestions();
// Set up UI components
JPanel panel = new JPanel(new GridLayout(3, 1));
questionLabel = new JLabel("Loading question...");
answerField = new JTextField();
timerLabel = new JLabel("Time: " + TIME_LIMIT_PER_QUESTION);
panel.add(questionLabel);
panel.add(answerField);
panel.add(timerLabel);
add(panel);
// Set up timer
remainingTime = TIME_LIMIT_PER_QUESTION;
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
remainingTime--;
timerLabel.setText("Time: " + remainingTime);
if (remainingTime <= 0) {
showNextQuestion(false);
}
}
});
// Show first question
showNextQuestion(true);
// Handle answer submission
answerField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
checkAnswer();
}
});
setVisible(true);
}
private void generateQuestions() {
Set<String> uniqueQuestions = new HashSet<>();
Random random = new Random();
while (questions.size() < NUMBER_OF_QUESTIONS) {
int type = random.nextInt(4); // 0: +, 1: -, 2: *, 3: /
int num1 = random.nextInt(100) + 1; // 1 to 100
int num2 = random.nextInt(100) + 1; // 1 to 100
int answer = 0;
String questionText = "";
switch (type) {
case 0: // Addition
answer = num1 + num2;
questionText = num1 + " + " + num2;
break;
case 1: // Subtraction
if (num1 >= num2) {
answer = num1 - num2;
questionText = num1 + " - " + num2;
}
break;
case 2: // Multiplication
answer = num1 * num2;
if (answer < 10000) {
questionText = num1 + " * " + num2;
}
break;
case 3: // Division
if (num2 != 0 && num1 % num2 == 0) {
answer = num1 / num2;
questionText = num1 + " / " + num2;
}
break;
}
if (!questionText.isEmpty() && uniqueQuestions.add(questionText)) {
questions.add(new ArithmeticQuestion(questionText, answer));
}
}
}
private void showNextQuestion(boolean resetTimer) {
if (currentQuestionIndex < questions.size()) {
ArithmeticQuestion question = questions.get(currentQuestionIndex++);
questionLabel.setText("Question: " + question.question);
answerField.setText("");
if (resetTimer) {
remainingTime = TIME_LIMIT_PER_QUESTION;
timerLabel.setText("Time: " + remainingTime);
timer.restart();
}
} else {
// Show results
JOptionPane.showMessageDialog(this, "Quiz finished!\nCorrect Answers: " + correctAnswers +
"\nWrong Answers: " + wrongAnswers +
"\nAccuracy: " + ((double) correctAnswers / (correctAnswers + wrongAnswers)) * 100 + "%");
System.exit(0);
}
}
private void checkAnswer() {
timer.stop();
ArithmeticQuestion currentQuestion = questions.get(currentQuestionIndex - 1);
try {
int userAnswer = Integer.parseInt(answerField.getText());
if (userAnswer == currentQuestion.answer) {
correctAnswers++;
JOptionPane.showMessageDialog(this, "Correct!");
} else {
wrongAnswers++;
JOptionPane.showMessageDialog(this, "Wrong! Correct answer was: " + currentQuestion.answer);
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter a valid number.");
wrongAnswers++;
}
showNextQuestion(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ArithmeticQuizGUI();
}
});
}
}
class ArithmeticQuestion {
String question;
int answer;
ArithmeticQuestion(String question, int answer) {
this.question = question;
this.answer = answer;
}
}
标签:,num1,num2,int,private,answer,new From: https://www.cnblogs.com/bixi/p/18442056