本文章主要是CSDN-问答板块,有题主提出的问题,我这边将完整代码提供出来,仅供大家参考学习!
一、效果截图
二、直接上代码
package com.example.dingtalk.question;import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;/**** 小学计算题生成*/
public class MathExerciseGenerator extends JFrame {private JComboBox<String> operationComboBox;private JButton generateButton;private JButton evaluateButton;private JButton showAnswersButton;private JTextArea questionArea;private JTextField answerField;public MathExerciseGenerator() {super("小学生数学练习题目生成系统");initComponents();}private void initComponents() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(new BorderLayout());operationComboBox = new JComboBox<>(new String[]{"10以内加法", "10以内减法", "20以内加法", "20以内减法", "100以内加法", "100以内减法", "100以内乘法", "100以内除法"});generateButton = new JButton("出题");evaluateButton = new JButton("评卷");showAnswersButton = new JButton("答案");questionArea = new JTextArea(20, 40);answerField = new JTextField(10);JPanel topPanel = new JPanel();topPanel.add(new JLabel("选择题目类型: "));topPanel.add(operationComboBox);topPanel.add(generateButton);topPanel.add(evaluateButton);topPanel.add(showAnswersButton);add(topPanel, BorderLayout.NORTH);add(new JScrollPane(questionArea), BorderLayout.CENTER);add(answerField, BorderLayout.SOUTH);generateButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {generateQuestions();}});evaluateButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {evaluateAnswers();}});showAnswersButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {showAnswers();}});pack();setLocationRelativeTo(null);}private void generateQuestions() {int numQuestions = 20;String operation = (String) operationComboBox.getSelectedItem();StringBuilder questions = new StringBuilder();StringBuilder answers = new StringBuilder(); // 用于存储答案Random random = new Random();for (int i = 0; i < numQuestions; i++) {int operand1 = random.nextInt(100) + 1;int operand2 = random.nextInt(100) + 1;String question = "";switch (operation) {case "10以内加法":operand1 = random.nextInt(10) + 1;operand2 = random.nextInt(10) + 1;question = operand1 + " + " + operand2 + " = ";break;case "10以内减法":operand1 = random.nextInt(10) + 1;operand2 = random.nextInt(operand1) + 1;question = operand1 + " - " + operand2 + " = ";break;case "20以内加法":operand1 = random.nextInt(20) + 1;operand2 = random.nextInt(20) + 1;question = operand1 + " + " + operand2 + " = ";break;case "20以内减法":operand1 = random.nextInt(20) + 1;operand2 = random.nextInt(operand1) + 1;question = operand1 + " - " + operand2 + " = ";break;case "100以内加法":operand1 = random.nextInt(100) + 1;operand2 = random.nextInt(100) + 1;question = operand1 + " + " + operand2 + " = ";break;case "100以内减法":operand1 = random.nextInt(100) + 1;operand2 = random.nextInt(operand1) + 1;question = operand1 + " - " + operand2 + " = ";break;case "100以内乘法":operand1 = random.nextInt(100) + 1;operand2 = random.nextInt(100) + 1;question = operand1 + " * " + operand2 + " = ";break;case "100以内除法":operand2 = random.nextInt(100) + 1;operand1 = operand2 * (random.nextInt(10) + 1);question = operand1 + " / " + operand2 + " = ";break;default:break;}questions.append(question).append("\n");// 生成并附加答案int result = calculateResult(operand1 + " " + getOperationSymbol(operation) + " " + operand2);answers.append(result).append("\n");}questionArea.setText(questions.toString());answerField.setText(answers.toString()); // 将答案设置到答案文本框}// 获取操作符private String getOperationSymbol(String operation) {switch (operation) {case "10以内加法":case "20以内加法":case "100以内加法":return "+";case "10以内减法":case "20以内减法":case "100以内减法":return "-";case "100以内乘法":return "*";case "100以内除法":return "/";default:return "";}}private void evaluateAnswers() {String[] answers = questionArea.getText().split("\n");String[] userResponses = answerField.getText().split(" ");StringBuilder evaluation = new StringBuilder();for (int i = 0; i < answers.length; i++) {if (i < userResponses.length) {String answer = extractAnswer(answers[i].trim()); // 从完整表达式中提取答案部分String userAnswer = userResponses[i].trim();String result = answer.equals(userAnswer) ? "√" : "x";evaluation.append(result).append("\n");}}JOptionPane.showMessageDialog(this, evaluation.toString(), "评卷结果", JOptionPane.INFORMATION_MESSAGE);}// 从完整表达式中提取答案部分private String extractAnswer(String fullExpression) {// 以等号分割表达式,并取第二部分作为答案String[] parts = fullExpression.split("=");if (parts.length == 2) {return parts[1].trim();} else {return "";}}private void showAnswers() {String[] questions = questionArea.getText().split("\n");StringBuilder correctAnswers = new StringBuilder();for (String question : questions) {String[] parts = question.split("=");if (parts.length == 2) {String expression = parts[0].trim();int result = calculateResult(expression);correctAnswers.append(expression).append(" = ").append(result).append("\n");}}JOptionPane.showMessageDialog(this, correctAnswers.toString(), "正确答案", JOptionPane.INFORMATION_MESSAGE);}private int calculateResult(String expression) {try {// 使用 ScriptEngineManager 计算表达式ScriptEngineManager manager = new ScriptEngineManager();ScriptEngine engine = manager.getEngineByName("js");return (int) engine.eval(expression);} catch (ScriptException e) {e.printStackTrace();}return 0;}public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {new MathExerciseGenerator().setVisible(true);}});}
}