编写一个算术测试软件,用来训练小学生的算术能力。程序有3个类组成,其中Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案是否正确。ComputerFrame对象负责为算术题目提供视图,例如用户可以通过ComputerFrame对象提供的GUI界面看到题目,并通过该GUI界面给出题目答案;MainClass是软件主类。
时间: 2023-05-31 10:04:57 浏览: 244
Teacher类:
public class Teacher {
private int num1;
private int num2;
private int operator;
public void generateQuestion() {
num1 = (int) (Math.random() * 100);
num2 = (int) (Math.random() * 100);
operator = (int) (Math.random() * 4);
}
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
public String getOperator() {
switch (operator) {
case 0:
return "+";
case 1:
return "-";
case 2:
return "*";
case 3:
return "/";
default:
return "";
}
}
public boolean isCorrect(int answer) {
int correctAnswer = 0;
switch (operator) {
case 0:
correctAnswer = num1 + num2;
break;
case 1:
correctAnswer = num1 - num2;
break;
case 2:
correctAnswer = num1 * num2;
break;
case 3:
correctAnswer = num1 / num2;
break;
}
return answer == correctAnswer;
}
}
ComputerFrame类:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ComputerFrame extends JFrame {
private Teacher teacher;
private JLabel questionLabel;
private JTextField answerField;
private JButton submitButton;
private JLabel resultLabel;
public ComputerFrame(Teacher teacher) {
this.teacher = teacher;
initUI();
}
private void initUI() {
setTitle("算术测试软件");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null);
questionLabel = new JLabel("");
questionLabel.setFont(new Font("SimSun", Font.PLAIN, 20));
answerField = new JTextField("", 10);
submitButton = new JButton("提交");
resultLabel = new JLabel("");
resultLabel.setFont(new Font("SimSun", Font.PLAIN, 20));
JPanel questionPanel = new JPanel(new FlowLayout());
questionPanel.add(questionLabel);
JPanel answerPanel = new JPanel(new FlowLayout());
answerPanel.add(new JLabel("你的答案:"));
answerPanel.add(answerField);
answerPanel.add(submitButton);
JPanel resultPanel = new JPanel(new FlowLayout());
resultPanel.add(resultLabel);
add(questionPanel, BorderLayout.NORTH);
add(answerPanel, BorderLayout.CENTER);
add(resultPanel, BorderLayout.SOUTH);
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int answer = Integer.parseInt(answerField.getText());
if (teacher.isCorrect(answer)) {
resultLabel.setText("回答正确!");
} else {
resultLabel.setText("回答错误!");
}
teacher.generateQuestion();
questionLabel.setText(teacher.getNum1() + " " + teacher.getOperator() + " " + teacher.getNum2() + " = ");
answerField.setText("");
} catch (NumberFormatException ex) {
resultLabel.setText("请输入数字!");
}
}
});
teacher.generateQuestion();
questionLabel.setText(teacher.getNum1() + " " + teacher.getOperator() + " " + teacher.getNum2() + " = ");
}
}
MainClass类:
public class MainClass {
public static void main(String[] args) {
Teacher teacher = new Teacher();
ComputerFrame frame = new ComputerFrame(teacher);
frame.setVisible(true);
}
}
阅读全文