在小学生算术四则运算测试系统 cmd 界面的基础上,使用 GUI 图形用户界 面知识将四则运算测试系统进行界面图形化设计。具体需求如下: 1.随机生成 10 道数学计算题,两个整数的四则运算;被减数一定要大于减 数,除数不能为 0,用户输入答案,正确给出提示,加入总分;不正确,给出提示。 10 道题做完给出最后总分和等级(小于 60 为不及格,60-70 为及格,70-80 为中, 80-90 为良,90-100 为优)。 2.界面要求: ① 能够正确显示 10 道测试题,每道题提供输入框供考生输入答案; ② 有“提交答案”按钮,当点击提交答案按钮时,给出每道题的答案,并 且对比考生的答案,显示正确或者错误信息; ③ 当点击提交答案按钮时,还统计考生的做题总分,本次考试获得的等级; ④ 有“重做”按钮,点击之后清空答案和做题记录,考生可重新开始作答; ⑤ 有“另做一套”题的按钮,点击之后重新出一套不一样的题; ⑥ 有“查看记录”按钮,点击按钮可查看考生的做题记录; ⑦ 有“退出测试”按钮,点击按钮后可退出整个测试系统。用Java写
时间: 2023-07-15 19:12:30 浏览: 56
好的,这个需求可以通过 Java 的图形用户界面库 Swing 来实现。下面是一个简单的实现:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class ArithmeticTest extends JFrame implements ActionListener {
private final int MAX_NUM = 100; // 生成的随机数最大值
private final int MAX_OP = 3; // 运算符数量
private final String[] OPERATORS = {"+", "-", "×", "÷"};
private final String[] GRADES = {"不及格", "及格", "中", "良", "优"};
private JLabel[] problems; // 显示题目的标签数组
private JTextField[] answers; // 答案输入框数组
private JButton submitButton, redoButton, newButton, recordButton, exitButton;
private int[] nums = new int[2]; // 随机生成的两个数
private int[] ops = new int[10]; // 随机生成的运算符
private int[] results = new int[10]; // 题目的正确答案
private int score = 0; // 总分
private boolean[] correct = new boolean[10]; // 记录每道题的答题情况
public ArithmeticTest() {
setTitle("小学生算术四则运算测试系统");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel problemPanel = new JPanel(new GridLayout(10, 3, 10, 5));
problems = new JLabel[30];
answers = new JTextField[10];
Random random = new Random();
for (int i = 0; i < 10; i++) {
nums[0] = random.nextInt(MAX_NUM) + 1;
nums[1] = random.nextInt(MAX_NUM) + 1;
ops[i] = random.nextInt(MAX_OP);
switch (ops[i]) {
case 0:
results[i] = nums[0] + nums[1];
break;
case 1:
if (nums[0] < nums[1]) {
int t = nums[0];
nums[0] = nums[1];
nums[1] = t;
}
results[i] = nums[0] - nums[1];
break;
case 2:
results[i] = nums[0] * nums[1];
break;
case 3:
nums[1] = random.nextInt(MAX_NUM - 1) + 1;
results[i] = nums[0] / nums[1];
nums[0] = nums[1] * results[i];
break;
}
problems[i * 3] = new JLabel(String.format("%d. ", i + 1));
problems[i * 3 + 1] = new JLabel(String.format("%d %s %d =", nums[0], OPERATORS[ops[i]], nums[1]));
answers[i] = new JTextField();
problems[i * 3 + 2] = new JLabel();
problemPanel.add(problems[i * 3]);
problemPanel.add(problems[i * 3 + 1]);
problemPanel.add(answers[i]);
}
JPanel buttonPanel = new JPanel(new FlowLayout());
submitButton = new JButton("提交答案");
submitButton.addActionListener(this);
buttonPanel.add(submitButton);
redoButton = new JButton("重新做题");
redoButton.addActionListener(this);
buttonPanel.add(redoButton);
newButton = new JButton("另做一套");
newButton.addActionListener(this);
buttonPanel.add(newButton);
recordButton = new JButton("查看记录");
recordButton.addActionListener(this);
buttonPanel.add(recordButton);
exitButton = new JButton("退出测试");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);
add(problemPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
for (int i = 0; i < 10; i++) {
try {
int ans = Integer.parseInt(answers[i].getText());
if (ans == results[i]) {
problems[i * 3 + 2].setText("正确");
correct[i] = true;
score += 10;
} else {
problems[i * 3 + 2].setText("错误");
correct[i] = false;
}
} catch (NumberFormatException ex) {
problems[i * 3 + 2].setText("未作答");
correct[i] = false;
}
}
JOptionPane.showMessageDialog(this, String.format("本次得分:%d,等级:%s", score, GRADES[score / 20 - 2]));
} else if (e.getSource() == redoButton) {
for (int i = 0; i < 10; i++) {
answers[i].setText("");
problems[i * 3 + 2].setText("");
}
score = 0;
for (int i = 0; i < 10; i++) {
correct[i] = false;
}
} else if (e.getSource() == newButton) {
for (int i = 0; i < 10; i++) {
nums[0] = new Random().nextInt(MAX_NUM) + 1;
nums[1] = new Random().nextInt(MAX_NUM) + 1;
ops[i] = new Random().nextInt(MAX_OP);
switch (ops[i]) {
case 0:
results[i] = nums[0] + nums[1];
break;
case 1:
if (nums[0] < nums[1]) {
int t = nums[0];
nums[0] = nums[1];
nums[1] = t;
}
results[i] = nums[0] - nums[1];
break;
case 2:
results[i] = nums[0] * nums[1];
break;
case 3:
nums[1] = new Random().nextInt(MAX_NUM - 1) + 1;
results[i] = nums[0] / nums[1];
nums[0] = nums[1] * results[i];
break;
}
problems[i * 3 + 1].setText(String.format("%d %s %d =", nums[0], OPERATORS[ops[i]], nums[1]));
problems[i * 3 + 2].setText("");
correct[i] = false;
}
score = 0;
} else if (e.getSource() == recordButton) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
if (correct[i]) {
sb.append("√");
} else {
sb.append("×");
}
}
JOptionPane.showMessageDialog(this, sb.toString());
} else if (e.getSource() == exitButton) {
System.exit(0);
}
}
public static void main(String[] args) {
ArithmeticTest test = new ArithmeticTest();
test.setVisible(true);
}
}
```
在这个实现中,我们使用了 `JPanel` 和 `JLabel` 等 Swing 组件来显示题目和答案,使用 `JTextField` 来接收用户的输入,使用 `JButton` 来响应用户的操作。在 `actionPerformed` 方法中,我们根据不同的按钮点击事件进行不同的处理,比如提交答案、重新做题、生成新的题目等。
这个实现还有很多可以改进的地方,比如将题目生成和显示的逻辑封装成一个方法,使用更加灵活的布局管理器等。