实验要求
编写一个算术测试的小软件,用来训练小学生的算术能力。程序由3个类组成,其中Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案是否正确,
ComputerFrame对象负责为算术题目提供视图,比如用户可以通过
ComputerFrame对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案,
MainClass
是软件主类。
代码
MainClass.java
public class MainClass {
public static void main(String[] args) {
ComputerFrame frame;
frame = new ComputerFrame();
frame.setTitle("算数测试");
frame.setBounds(100, 100, 650, 180);
}
}
ComputerFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComputerFrame extends JFrame {
JMenuBar menubar;
JMenu choiceGrade;
JMenuItem grade1, grade2;
JTextField textOne, textTwo, textResult;
JButton getProblem, giveAnswer;
JLabel operatorLabel, message;
Teacher teacherzhang;
ComputerFrame() {
teacherzhang = new Teacher();
teacherzhang.setMaxInteger(20);
setLayout(new FlowLayout());
menubar = new JMenuBar();
choiceGrade = new JMenu("选择级别");
grade1 = new JMenuItem("幼儿级别");
grade2 = new JMenuItem("儿童级别");
grade1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
teacherzhang.setMaxInteger(10);
}
});
grade2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
teacherzhang.setMaxInteger(50);
}
});
choiceGrade.add(grade1);
choiceGrade.add(grade2);
menubar.add(choiceGrade);
setJMenuBar(menubar);
textOne = new JTextField(5);
textTwo = new JTextField(5);
textResult = new JTextField(5);
operatorLabel = new JLabel("+");
operatorLabel.setFont(new Font("Arial", Font.BOLD, 20));
message = new JLabel("你还没有回答");
getProblem = new JButton("获取题目");
giveAnswer = new JButton("确认答案");
add(textOne);
add(operatorLabel);
add(textTwo);
add(new JLabel("="));
add(textResult);
add(giveAnswer);
add(message);
add(getProblem);
textResult.requestFocus();
textOne.setEditable(false);
textTwo.setEditable(false);
getProblem.setActionCommand("getProblem");
textResult.setActionCommand("answer");
giveAnswer.setActionCommand("answer");
teacherzhang.setJTextField(textOne, textTwo, textResult);
teacherzhang.setJLabel(operatorLabel, message);
getProblem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
teacherzhang.actionPerformed(e);
}
});
giveAnswer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
teacherzhang.actionPerformed(e);
}
});
textResult.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
teacherzhang.actionPerformed(e);
}
});
setVisible(true);
validate();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
Teacher.java
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Teacher {
int numberOne, numberTwo;
String operator = "";
boolean isRight;
Random random;
int maxInteger;
JTextField textOne, textTwo, textResult;
JLabel operatorLabel, message;
Teacher() {
random = new Random();
}
public void setMaxInteger(int n) {
maxInteger = n;
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("getProblem")) {
numberOne = random.nextInt(maxInteger) + 1;
numberTwo = random.nextInt(maxInteger) + 1;
double d = Math.random();
if (d >= 0.5)
operator = "+";
else
operator = "-";
textOne.setText("" + numberOne);
textTwo.setText("" + numberTwo);
operatorLabel.setText(operator);
message.setText("请回答");
textResult.setText(null);
} else if (str.equals("answer")) {
String answer = textResult.getText();
try {
int result = Integer.parseInt(answer);
if (operator.equals("+")) {
if (result == numberOne + numberTwo)
message.setText("你的回答正确");
else
message.setText("你的回答错误");
} else if (operator.equals("-")) {
if (result == numberOne - numberTwo)
message.setText("你的回答正确");
else
message.setText("你的回答错误");
}
} catch (NumberFormatException ex) {
message.setText("请输入数字字符");
}
}
}
public void setJTextField(JTextField... t) {
textOne = t[0];
textTwo = t[1];
textResult = t[2];
}
public void setJLabel(JLabel... label) {
operatorLabel = label[0];
message = label[1];
}
}
运算结果
版权声明:本文为mcp3128原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。