package kousuanti; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class ArithmeticProgram extends JFrame { private JPanel contentPanel; private JLabel[] questionLabels; private JTextField[] answerFields; private JButton regenerateButton; private JButton submitButton; private JLabel resultLabel; public ArithmeticProgram() { contentPanel = new JPanel(); contentPanel.setLayout(new GridLayout(10, 2)); // 一行两个元素,题目和作答区域 questionLabels = new JLabel[10]; answerFields = new JTextField[10]; for (int i = 0; i < 10; i++) { int num1 = generateRandomNumber(); int num2 = generateRandomNumber(); char operator = generateRandomOperator(); int result = calculateResult(num1, num2, operator); JLabel questionLabel = new JLabel("第" + (i+1) + "题: " + num1 + " " + operator + " " + num2 + " = "); questionLabels[i] = questionLabel; JTextField answerField = new JTextField(); answerFields[i] = answerField; contentPanel.add(questionLabel); contentPanel.add(answerField); } regenerateButton = new JButton("重置"); submitButton = new JButton("提交"); regenerateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { regenerateQuestions(); } }); submitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { checkAnswers(); } }); resultLabel = new JLabel(); JPanel buttonPanel = new JPanel(); buttonPanel.add(regenerateButton); buttonPanel.add(submitButton); add(contentPanel, BorderLayout.CENTER); add(resultLabel, BorderLayout.SOUTH); add(buttonPanel, BorderLayout.NORTH); setTitle("口算"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } private int generateRandomNumber() { Random random = new Random(); return random.nextInt(60) + 1; // 生成1到10的随机数 } private char generateRandomOperator() { Random random = new Random(); int op = random.nextInt(2); return (op == 0) ? '+' : '-'; } private int calculateResult(int num1, int num2, char operator) { if (operator == '+') { return num1 + num2; } else { return num1 - num2; } } private void regenerateQuestions() { for (int i = 0; i < 10; i++) { int num1 = generateRandomNumber(); int num2 = generateRandomNumber(); char operator = generateRandomOperator(); questionLabels[i].setText("Question " + (i+1) + ": " + num1 + " " + operator + " " + num2 + " = "); answerFields[i].setText(""); } resultLabel.setText(""); } private void checkAnswers() { StringBuilder wrongAnswers = new StringBuilder(); for (int i = 0; i < 10; i++) { String userAnswer = answerFields[i].getText().trim(); int num1 = Integer.parseInt(questionLabels[i].getText().split("\\s+")[1]); char operator = questionLabels[i].getText().charAt(questionLabels[i].getText().length()-5); int num2 = Integer.parseInt(questionLabels[i].getText().split("\\s+")[3]); int correctResult = calculateResult(num1, num2, operator); if (!userAnswer.isEmpty() && Integer.parseInt(userAnswer) != correctResult) { wrongAnswers.append("Question ").append(i + 1).append(", "); } } if (wrongAnswers.length() == 0) { resultLabel.setText("All answers are correct!"); } else { resultLabel.setText("Wrong answers: " + wrongAnswers.toString()); } } public static void main(String[] args) { new ArithmeticProgram(); } }
标签:总结,23.11,num1,22,int,private,operator,new,num2 From: https://www.cnblogs.com/laobing6214/p/17852645.html