用java完成四则运算测试 代码行数超过500行 功能要求:该程序实现⼗道100以内四则运算数学题,能根据题⽬计算出答案,与输⼊答案对⽐,判断做题是否正确,最后计算分数。 界⾯要求:图形界⾯实现,基于ArrayList、Vector等⾼级类。
时间: 2023-12-24 17:05:04 浏览: 114
以下是一个示例代码:
```
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ArithmeticTest {
private JFrame frame;
private JTextField answerField;
private JLabel problemLabel;
private JLabel scoreLabel;
private ArrayList<String> problems;
private ArrayList<Integer> answers;
private int currentIndex;
private int score;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ArithmeticTest window = new ArithmeticTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ArithmeticTest() {
initialize();
generateProblems();
showProblem();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
problemLabel = new JLabel("");
problemLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
GridBagConstraints gbc_problemLabel = new GridBagConstraints();
gbc_problemLabel.insets = new Insets(0, 0, 20, 0);
gbc_problemLabel.gridx = 0;
gbc_problemLabel.gridy = 0;
gbc_problemLabel.fill = GridBagConstraints.HORIZONTAL;
gbc_problemLabel.anchor = GridBagConstraints.NORTH;
frame.getContentPane().add(problemLabel, gbc_problemLabel);
answerField = new JTextField();
answerField.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
GridBagConstraints gbc_answerField = new GridBagConstraints();
gbc_answerField.insets = new Insets(0, 0, 20, 0);
gbc_answerField.gridx = 0;
gbc_answerField.gridy = 1;
gbc_answerField.fill = GridBagConstraints.HORIZONTAL;
gbc_answerField.anchor = GridBagConstraints.NORTH;
frame.getContentPane().add(answerField, gbc_answerField);
answerField.setColumns(10);
JPanel buttonPanel = new JPanel();
GridBagConstraints gbc_buttonPanel = new GridBagConstraints();
gbc_buttonPanel.gridx = 0;
gbc_buttonPanel.gridy = 2;
gbc_buttonPanel.fill = GridBagConstraints.HORIZONTAL;
gbc_buttonPanel.anchor = GridBagConstraints.NORTH;
frame.getContentPane().add(buttonPanel, gbc_buttonPanel);
JButton submitButton = new JButton("提交");
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkAnswer();
}
});
buttonPanel.add(submitButton);
JButton nextButton = new JButton("下一题");
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showProblem();
}
});
buttonPanel.add(nextButton);
scoreLabel = new JLabel("得分:0");
GridBagConstraints gbc_scoreLabel = new GridBagConstraints();
gbc_scoreLabel.gridx = 0;
gbc_scoreLabel.gridy = 3;
gbc_scoreLabel.fill = GridBagConstraints.HORIZONTAL;
gbc_scoreLabel.anchor = GridBagConstraints.NORTH;
frame.getContentPane().add(scoreLabel, gbc_scoreLabel);
}
/**
* Generate 10 arithmetic problems with two operands and one operator.
*/
private void generateProblems() {
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 op = random.nextInt(4); // 0: +, 1: -, 2: *, 3: /
String problem = buildProblemString(a, b, op);
int answer = calculateAnswer(a, b, op);
problems.add(problem);
answers.add(answer);
}
}
/**
* Build the string representation of an arithmetic problem.
*/
private String buildProblemString(int a, int b, int op) {
String operator = "";
switch (op) {
case 0:
operator = "+";
break;
case 1:
operator = "-";
break;
case 2:
operator = "*";
break;
case 3:
operator = "/";
break;
}
return a + " " + operator + " " + b + " = ";
}
/**
* Calculate the answer of an arithmetic problem.
*/
private int calculateAnswer(int a, int b, int op) {
switch (op) {
case 0:
return a + b;
case 1:
return a - b;
case 2:
return a * b;
case 3:
return a / b;
default:
return 0;
}
}
/**
* Show the current problem.
*/
private void showProblem() {
if (currentIndex >= problems.size()) {
JOptionPane.showMessageDialog(frame, "测试已结束,你的得分是:" + score);
return;
}
String problem = problems.get(currentIndex);
problemLabel.setText(problem);
answerField.setText("");
}
/**
* Check the answer of the current problem.
*/
private void checkAnswer() {
if (currentIndex >= problems.size()) {
return;
}
String answerText = answerField.getText();
if (answerText.isEmpty()) {
JOptionPane.showMessageDialog(frame, "请输入答案");
return;
}
int answer = Integer.parseInt(answerText);
int correctAnswer = answers.get(currentIndex);
if (answer == correctAnswer) {
score++;
scoreLabel.setText("得分:" + score);
JOptionPane.showMessageDialog(frame, "回答正确!");
} else {
JOptionPane.showMessageDialog(frame, "回答错误,正确答案是:" + correctAnswer);
}
currentIndex++;
showProblem();
}
}
```
这个程序使用了Swing库实现了图形界面,使用了ArrayList存储题目和答案等数据。生成题目时,使用了Random类随机生成两个操作数和一个运算符,然后根据运算符计算出答案;显示题目时,从ArrayList中取出当前问题并在界面上显示;检查答案时,从文本框中读取输入的答案,与正确答案进行比较,并更新得分和界面。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)