小学算术运算测试程序java_Java实现小学四则运算练习系统(UI)
时间: 2024-01-05 18:03:35 浏览: 160
以下是一个简单的小学四则运算练习系统的Java实现,带有UI界面:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ArithmeticPractice extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel num1Label, num2Label, operatorLabel, resultLabel, feedbackLabel;
private JTextField resultField;
private JButton submitButton, nextButton, quitButton;
private JPanel questionPanel, inputPanel, buttonPanel;
private Random random = new Random();
private int num1, num2, result, operator;
public ArithmeticPractice() {
super("小学四则运算练习系统");
// 初始化题目
generateQuestion();
// 初始化UI组件
num1Label = new JLabel(Integer.toString(num1));
num2Label = new JLabel(Integer.toString(num2));
operatorLabel = new JLabel(getOperatorSymbol(operator));
resultLabel = new JLabel("=");
feedbackLabel = new JLabel(" ");
num1Label.setFont(new Font("Arial", Font.BOLD, 48));
num2Label.setFont(new Font("Arial", Font.BOLD, 48));
operatorLabel.setFont(new Font("Arial", Font.BOLD, 48));
resultLabel.setFont(new Font("Arial", Font.BOLD, 48));
feedbackLabel.setFont(new Font("Arial", Font.BOLD, 24));
resultField = new JTextField(5);
resultField.setFont(new Font("Arial", Font.BOLD, 48));
submitButton = new JButton("提交");
nextButton = new JButton("下一题");
quitButton = new JButton("退出");
submitButton.addActionListener(this);
nextButton.addActionListener(this);
quitButton.addActionListener(this);
questionPanel = new JPanel(new FlowLayout());
questionPanel.add(num1Label);
questionPanel.add(operatorLabel);
questionPanel.add(num2Label);
questionPanel.add(resultLabel);
inputPanel = new JPanel(new FlowLayout());
inputPanel.add(resultField);
buttonPanel = new JPanel(new GridLayout(1, 3));
buttonPanel.add(submitButton);
buttonPanel.add(nextButton);
buttonPanel.add(quitButton);
JPanel feedbackPanel = new JPanel(new FlowLayout());
feedbackPanel.add(feedbackLabel);
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
contentPanel.add(questionPanel, BorderLayout.NORTH);
contentPanel.add(inputPanel, BorderLayout.CENTER);
contentPanel.add(buttonPanel, BorderLayout.SOUTH);
contentPanel.add(feedbackPanel, BorderLayout.PAGE_END);
setContentPane(contentPanel);
setSize(600, 300);
setLocationRelativeTo(null);
}
private void generateQuestion() {
num1 = random.nextInt(100) + 1;
num2 = random.nextInt(100) + 1;
operator = random.nextInt(4);
switch (operator) {
case 0:
result = num1 + num2;
break;
case 1:
result = num1 - num2;
break;
case 2:
result = num1 * num2;
break;
case 3:
result = num1 / num2;
num1 = result * num2; // 保证除法结果是整数
break;
}
}
private String getOperatorSymbol(int operator) {
switch (operator) {
case 0:
return "+";
case 1:
return "-";
case 2:
return "×";
case 3:
return "÷";
default:
return "";
}
}
private void checkAnswer() {
try {
int answer = Integer.parseInt(resultField.getText());
if (answer == result) {
feedbackLabel.setForeground(Color.GREEN);
feedbackLabel.setText("回答正确!");
} else {
feedbackLabel.setForeground(Color.RED);
feedbackLabel.setText("回答错误,正确答案是:" + result);
}
} catch (NumberFormatException e) {
feedbackLabel.setForeground(Color.RED);
feedbackLabel.setText("请输入一个整数!");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
checkAnswer();
} else if (e.getSource() == nextButton) {
generateQuestion();
num1Label.setText(Integer.toString(num1));
num2Label.setText(Integer.toString(num2));
operatorLabel.setText(getOperatorSymbol(operator));
feedbackLabel.setText("");
resultField.setText("");
resultField.requestFocus();
} else if (e.getSource() == quitButton) {
System.exit(0);
}
}
public static void main(String[] args) {
ArithmeticPractice app = new ArithmeticPractice();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
```
这个程序使用了Java Swing库实现了一个简单的UI界面。每次打开程序会随机生成一道题目,你需要在文本框中输入你的答案,然后点击“提交”按钮进行检查。如果答案正确,会在下方的反馈栏中显示“回答正确!”,否则会显示正确答案。点击“下一题”按钮会生成新的题目,点击“退出”按钮可以退出程序。
阅读全文