编写一个算术测试小软件,用来训练小学生的算术能力。软件界面如下图所示,点击“获取题目”的按钮,监视器负责给出算术题目显示在文本框和标签上。用户在第三个文本框输入答案,点击“确认答案”按钮,系统给出结果“回答正确”或者“回答错误”。java程序
时间: 2024-06-09 17:07:30 浏览: 129
以下是一个简单的算术测试小软件的Java代码实现,希望能够帮助你。
```java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
public class ArithmeticTest extends JFrame {
private JPanel contentPane;
private JTextField textFieldNum1;
private JTextField textFieldNum2;
private JTextField textFieldOperator;
private JTextField textFieldAnswer;
private JLabel labelResult;
private int num1;
private int num2;
private int operator;
private int answer;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ArithmeticTest frame = new ArithmeticTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ArithmeticTest() {
setTitle("算术测试");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("第一个数:");
lblNewLabel.setBounds(54, 31, 68, 15);
contentPane.add(lblNewLabel);
textFieldNum1 = new JTextField();
textFieldNum1.setEditable(false);
textFieldNum1.setBounds(132, 28, 66, 21);
contentPane.add(textFieldNum1);
textFieldNum1.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("第二个数:");
lblNewLabel_1.setBounds(54, 74, 68, 15);
contentPane.add(lblNewLabel_1);
textFieldNum2 = new JTextField();
textFieldNum2.setEditable(false);
textFieldNum2.setBounds(132, 71, 66, 21);
contentPane.add(textFieldNum2);
textFieldNum2.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("运算符:");
lblNewLabel_2.setBounds(54, 119, 68, 15);
contentPane.add(lblNewLabel_2);
textFieldOperator = new JTextField();
textFieldOperator.setEditable(false);
textFieldOperator.setBounds(132, 116, 66, 21);
contentPane.add(textFieldOperator);
textFieldOperator.setColumns(10);
JButton btnNewButton = new JButton("获取题目");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
generateQuestion();
}
});
btnNewButton.setBounds(54, 163, 93, 23);
contentPane.add(btnNewButton);
JLabel lblNewLabel_3 = new JLabel("你的答案:");
lblNewLabel_3.setBounds(54, 207, 68, 15);
contentPane.add(lblNewLabel_3);
textFieldAnswer = new JTextField();
textFieldAnswer.setBounds(132, 204, 66, 21);
contentPane.add(textFieldAnswer);
textFieldAnswer.setColumns(10);
JButton btnNewButton_1 = new JButton("确认答案");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkAnswer();
}
});
btnNewButton_1.setBounds(222, 163, 93, 23);
contentPane.add(btnNewButton_1);
labelResult = new JLabel("");
labelResult.setBounds(222, 207, 93, 15);
contentPane.add(labelResult);
}
private void generateQuestion() {
Random random = new Random();
num1 = random.nextInt(100);
num2 = random.nextInt(100);
operator = random.nextInt(4);
switch (operator) {
case 0:
textFieldOperator.setText("+");
answer = num1 + num2;
break;
case 1:
textFieldOperator.setText("-");
answer = num1 - num2;
break;
case 2:
textFieldOperator.setText("*");
answer = num1 * num2;
break;
case 3:
textFieldOperator.setText("/");
answer = num1 / num2;
break;
}
textFieldNum1.setText(String.valueOf(num1));
textFieldNum2.setText(String.valueOf(num2));
textFieldAnswer.setText("");
labelResult.setText("");
}
private void checkAnswer() {
try {
int userAnswer = Integer.parseInt(textFieldAnswer.getText());
if (userAnswer == answer) {
labelResult.setText("回答正确");
} else {
labelResult.setText("回答错误");
}
} catch (NumberFormatException e) {
labelResult.setText("请输入数字");
}
}
}
```
这个程序使用了Java Swing库来创建图形用户界面,并使用了Random类来生成随机的算术题目。generateQuestion方法用于生成新的算术题目,并在文本框和标签中显示它们。checkAnswer方法用于检查用户输入的答案是否正确,并在标签中显示结果。
阅读全文