在这里给出一个示例代码,这是使用 Java Swing 实现的简单算式测试程序,可以供您参考:
``` import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random;
public class Quiz extends JFrame implements ActionListener {
// 定义 5 个算式和答案 private int[][] equations = new int[][] { {3, 4, 7}, {5, 5, 10}, {7, 8, 15}, {9, 10, 19}, {11, 12, 23} };
// 定义 5 个文本框,用于输入结果 private JTextField[] answers = new JTextField[5];
public Quiz() { // 设置窗口标题 setTitle("算式测试");
// 设置布局
setLayout(new GridLayout(6, 2));
// 循环显示算式
for (int i = 0; i < 5; i++) {
// 显示算式
add(new JLabel(equations[i][0] + " + " + equations[i][1] + " = "));
// 创建文本框
answers[i] = new JTextField(10);
// 添加文本框
add(answers[i]);
}
// 创建检查按钮
JButton checkButton = new JButton("检查");
// 添加按钮监听器
checkButton.addActionListener(this);
// 添加按钮
add(checkButton);
// 设置窗口大小
setSize(400, 200);
// 设置窗口可见
setVisible(true);
}
public static void main(String[] args) { new Quiz(); }
@Override public void actionPerformed(ActionEvent e) { // 统计分数 int score = 0; // 循环检查答案 for (int i = 0; i < 5; i++) { // 获取文本框中的答案 int answer = Integer.parseInt