完成了一个能够列出30道四则运算的java程序,
题目要求:乘法不超过四位数,减法大于零,除法结果为整数;
实现可视化界面,并且能够计算得分与计时;
点击查看代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class MathQuiz2 extends JFrame {
private static final int NUM_QUESTIONS = 30;
private static final int TIME_LIMIT = 60; // 60 seconds
private JTextArea questionArea;
private JTextField answerField;
private JButton submitButton;
private JLabel timerLabel;
private int currentQuestionIndex = 0;
private int score = 0;
private Question[] questions;
private Timer timer;
public MathQuiz2() {
setTitle("Math Quiz");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
questionArea = new JTextArea();
questionArea.setEditable(false);
questionArea.setLineWrap(true);
questionArea.setWrapStyleWord(true);
add(new JScrollPane(questionArea), BorderLayout.CENTER);
JPanel inputPanel = new JPanel();
answerField = new JTextField(10);
submitButton = new JButton("提交答案");
timerLabel = new JLabel("剩余时间: " + TIME_LIMIT);
inputPanel.add(answerField);
inputPanel.add(submitButton);
inputPanel.add(timerLabel);
add(inputPanel, BorderLayout.SOUTH);
submitButton.addActionListener(new SubmitAnswerListener());
generateQuestions();
displayNextQuestion();
startTimer();
setVisible(true);
}
private void generateQuestions() {
questions = new Question[NUM_QUESTIONS];
Random random = new Random();
for (int i = 0; i < NUM_QUESTIONS; i++) {
int a = random.nextInt(100); // 0-99
int b = random.nextInt(100); // 0-99
int operation = random.nextInt(4); // 0:加, 1:减, 2:乘, 3:除
switch (operation) {
case 0: // 加法
questions[i] = new Question(a + " + " + b, a + b);
break;
case 1: // 减法
if (a >= b) {
questions[i] = new Question(a + " - " + b, a - b);
} else {
i--; // 重新生成题目
}
break;
case 2: // 乘法
int c = random.nextInt(100); // 0-99
if (a * b <= 9999) {
questions[i] = new Question(a + " * " + b, a * b);
} else {
i--; // 重新生成题目
}
break;
case 3: // 除法
if (b != 0 && a % b == 0) {
questions[i] = new Question(a + " / " + b, a / b);
} else {
i--; // 重新生成题目
}
break;
}
}
}
private void displayNextQuestion() {
if (currentQuestionIndex < NUM_QUESTIONS) {
questionArea.setText(questions[currentQuestionIndex].getQuestion());
answerField.setText("");
answerField.requestFocus();
} else {
endQuiz();
}
}
private void startTimer() {
timer = new Timer(1000, new ActionListener() {
private int timeLeft = TIME_LIMIT;
@Override
public void actionPerformed(ActionEvent e) {
if (timeLeft > 0) {
timeLeft--;
timerLabel.setText("剩余时间: " + timeLeft);
} else {
((Timer) e.getSource()).stop();
endQuiz();
}
}
});
timer.start();
}
private void endQuiz() {
timer.stop();
JOptionPane.showMessageDialog(this, "测验结束!您的得分是: " + score + "/" + NUM_QUESTIONS);
System.exit(0);
}
private class SubmitAnswerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
int answer = Integer.parseInt(answerField.getText());
if (answer == questions[currentQuestionIndex].getAnswer()) {
score++;
}
currentQuestionIndex++;
displayNextQuestion();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(MathQuiz2.this, "请输入有效的数字!");
}
}
}
private static class Question {
private final String question;
private final int answer;
public Question(String question, int answer) {
this.question = question;
this.answer = answer;
}
public String getQuestion() {
return question;
}
public int getAnswer() {
return answer;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(MathQuiz2::new);
}
}