今天上了Java课,老师让生成对话框,不怎么会,该学学的,下面是代码:
import java.util.Random;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Method {
public String[][] generateQuestions() {
Random rand = new Random();
String[] questions = new String[30];
String[] answers = new String[30];
for (int i = 0; i < 30; i++) {
int fushu = rand.nextInt(100);
int m = rand.nextInt(100) - fushu;
int n = rand.nextInt(100) - fushu;
int q = rand.nextInt(100);
int o = rand.nextInt(4);
int p = rand.nextInt(10); // 更改此处以限制范围
switch (o) {
case 0: // 加法
questions[i] = n > 0 ? m + "+" + n + "=" : m + "" + n + "=";
answers[i] = String.valueOf(m + n);
break;
case 1: // 减法
questions[i] = m + "-" + q + "=";
answers[i] = String.valueOf(m - q);
break;
case 2: // 乘法
int multiplier, multiplicand;
do {
multiplier = rand.nextInt(10) + 1; // 单个乘数范围 [1, 10]
multiplicand = rand.nextInt(10) + 1; // 单个乘数范围 [1, 10]
} while (multiplier * multiplicand > 999);
questions[i] = multiplier + "*" + multiplicand + "=";
answers[i] = String.valueOf(multiplier * multiplicand);
break;
case 3: // 除法
int dividend;
do {
dividend = rand.nextInt(100);
int divisor = rand.nextInt(99) + 1; // g can't be 0
if (dividend % divisor == 0) {
questions[i] = dividend + "/" + divisor + "=";
answers[i] = String.valueOf(dividend / divisor);
break;
}
} while (true);
break;
}
}
return new String[][]{questions, answers}; // 返回问题和答案
}
}
public class Math4 {
public static void main(String[] args) {
Method method = new Method();
String[][] questionsAndAnswers = method.generateQuestions();
String[] questions = questionsAndAnswers[0];
String[] answers = questionsAndAnswers[1];
// 创建自定义的JFrame
JFrame frame = new JFrame("整数四则运算测试");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, questions.length * 50 + 150); // 根据问题数量动态设置高度
frame.setLayout(new GridLayout(questions.length + 1, 2)); // 多一行用于倒计时显示
// 添加问题和输入框到帧中
JTextField[] answerFields = new JTextField[questions.length]; // 保存所有答案文本框
for (int i = 0; i < questions.length; i++) {
JLabel questionLabel = new JLabel(questions[i]);
answerFields[i] = new JTextField(15);
frame.add(questionLabel);
frame.add(answerFields[i]);
}
// 添加倒计时标签
JLabel timerLabel = new JLabel("剩余时间: 30 秒");
frame.add(timerLabel);
// 创建提交按钮
JButton submitButton = new JButton("提交所有答案");
frame.add(submitButton); // 将按钮添加到框架
// 提交按钮的动作事件
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StringBuilder answersResult = new StringBuilder();
int correctCount = 0;
int incorrectCount = 0;
for (int i = 0; i < questions.length; i++) {
String userAnswer = answerFields[i].getText();
if (userAnswer.equals(answers[i])) {
correctCount++;
} else {
incorrectCount++;
answersResult.append(questions[i] + " 正确答案: " + answers[i] + "\n");
}
}
JOptionPane.showMessageDialog(frame,
"正确数量: " + correctCount + "\n" +
"错误数量: " + incorrectCount + "\n" +
"错误详情:\n" + answersResult.toString(),
"提交的答案",
JOptionPane.INFORMATION_MESSAGE);
}
});
// 倒计时逻辑
final int countdownTime = 120; // 倒计时秒数
Timer timer = new Timer(1000, new ActionListener() {
int timeLeft = countdownTime;
@Override
public void actionPerformed(ActionEvent e) {
if (timeLeft > 0) {
timerLabel.setText("剩余时间: " + timeLeft + " 秒");
timeLeft--;
} else {
((Timer) e.getSource()).stop();
JOptionPane.showMessageDialog(frame, "时间到!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 禁用文本框和按钮
for (Component component : frame.getContentPane().getComponents()) {
if (component instanceof JTextField) {
component.setEnabled(false);
}
}
submitButton.setEnabled(false);
}
}
});
frame.setVisible(true); // 显示框架
timer.start(); // 启动计时器
}
}