用Java写出四则运算测试 功能要求:该程序实现⼗道100以内四则运算数学题,能根据题⽬计算出答案,与输⼊答案对⽐,判断做题是否正确,最后计算分数。 界⾯要求:图形界⾯实现,基于ArrayList、Vector等⾼级类。
时间: 2023-12-20 13:07:59 浏览: 40
好的,以下是一个简单的Java代码实现:
```java
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ArithmeticTest extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private ArrayList<String> problems;
private ArrayList<Integer> answers;
private int score = 0;
public static void main(String[] args) {
ArithmeticTest frame = new ArithmeticTest();
frame.setVisible(true);
}
public ArithmeticTest() {
setTitle("四则运算测试");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("题目:");
lblNewLabel.setBounds(10, 10, 54, 15);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(74, 7, 350, 21);
textField.setEditable(false);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("答案:");
lblNewLabel_1.setBounds(10, 45, 54, 15);
contentPane.add(lblNewLabel_1);
JTextField answerField = new JTextField();
answerField.setBounds(74, 42, 96, 21);
contentPane.add(answerField);
answerField.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("得分:0");
lblNewLabel_2.setBounds(10, 80, 54, 15);
contentPane.add(lblNewLabel_2);
problems = new ArrayList<String>();
answers = new ArrayList<Integer>();
Random random = new Random();
for (int i = 0; i < 10; i++) {
int a = random.nextInt(100);
int b = random.nextInt(100);
int operator = random.nextInt(4);
String problem = "";
int answer = 0;
switch (operator) {
case 0:
problem = a + " + " + b + " = ";
answer = a + b;
break;
case 1:
problem = a + " - " + b + " = ";
answer = a - b;
break;
case 2:
problem = a + " * " + b + " = ";
answer = a * b;
break;
case 3:
if (b == 0) {
b = 1;
}
problem = a + " / " + b + " = ";
answer = a / b;
break;
}
problems.add(problem);
answers.add(answer);
}
showProblem(problems.get(0));
answerField.addActionListener(e -> {
String answerString = answerField.getText().trim();
if (!answerString.matches("[0-9]+")) {
JOptionPane.showMessageDialog(this, "请输入数字!");
return;
}
int answer = Integer.parseInt(answerString);
int currentIndex = score / 10;
if (answer == answers.get(currentIndex)) {
score += 10;
lblNewLabel_2.setText("得分:" + score);
currentIndex++;
if (currentIndex >= 10) {
JOptionPane.showMessageDialog(this, "恭喜你,完成测试!得分:" + score);
System.exit(0);
} else {
showProblem(problems.get(currentIndex));
answerField.setText("");
}
} else {
JOptionPane.showMessageDialog(this, "答案不正确!");
}
});
}
private void showProblem(String problem) {
textField.setText(problem);
}
}
```
这个程序使用了Swing图形界面库来实现用户交互界面,使用了ArrayList类来动态存储题目和答案,运用了Java 8的Lambda表达式来简化代码。具体实现如下:
首先,我们需要定义一个JFrame作为窗口,然后在窗口中添加一个JPanel作为内容面板,最后在内容面板中添加需要的组件,如题目、答案输入框、得分等。在构造函数中,我们使用Random类生成10道100以内的四则运算题目,并将题目和答案保存在problems和answers这两个ArrayList中。
接着,我们在答案输入框中添加了一个ActionListener监听器,当用户输入答案时,程序会根据答案是否正确来更新得分并显示下一题。当用户完成全部10道题目时,程序会弹出一个提示框显示得分,并退出程序。
最后,我们使用了Java的界面布局管理器来对组件进行布局,这样就可以保证程序在不同平台上的显示效果基本一致。