12月20日
今天用javaswing写了一个随机生成计算题的题目
主程序
import javax.swing.*; public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new MathExerciseGenerator(); } }); } }
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 JTextArea exerciseArea, resultArea; private JTextField answerField; public MathExerciseGenerator() { // 设置窗体属性 setTitle("BYD"); setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建组件 operationComboBox = new JComboBox<>(new String[]{"10以内加法", "10以内减法", "20以内加法", "20以内减法", "100以内加法", "100以内减法","100以内乘法","100以内除法"}); exerciseArea = new JTextArea(10, 30); resultArea = new JTextArea(10, 30); resultArea.setEditable(false); answerField = new JTextField(20); JButton generateButton = new JButton("出题"); JButton checkButton = new JButton("评卷"); JButton showAnswerButton = new JButton("答案"); generateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { generateExercises(); } }); checkButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { checkAnswers(); } }); showAnswerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showAnswers(); } }); // 布局管理 setLayout(new FlowLayout()); add(new JLabel("题目类型:")); add(operationComboBox); add(generateButton); add(checkButton); add(showAnswerButton); add(new JScrollPane(exerciseArea)); add(new JLabel("你的答案:")); add(answerField); add(new JScrollPane(resultArea)); setVisible(true); } private void generateExercises() { String operation = (String) operationComboBox.getSelectedItem(); StringBuilder exercises = new StringBuilder(); Random random = new Random(); int operand1, operand2; for (int i = 0; i < 20; i++) { switch (operation) { case "10以内加法": operand1 = random.nextInt(10)+1; operand2 = random.nextInt(11 - operand1); exercises.append(operand1).append(" + ").append(operand2).append(" =\n"); break; case "10以内减法": operand1 = random.nextInt(10)+1; operand2 = random.nextInt(operand1 + 1); exercises.append(operand1).append(" - ").append(operand2).append(" =\n"); break; case "20以内加法": operand1 = random.nextInt(20)+1; operand2 = random.nextInt(21 - operand1); exercises.append(operand1).append(" + ").append(operand2).append(" =\n"); break; case "20以内减法": operand1 = random.nextInt(20)+1; operand2 = random.nextInt(operand1 + 1); exercises.append(operand1).append(" - ").append(operand2).append(" =\n"); break; case "100以内加法": operand1 = random.nextInt(100)+1; operand2 = random.nextInt(101 - operand1); exercises.append(operand1).append(" + ").append(operand2).append(" =\n"); break; case "100以内减法": operand1 = random.nextInt(100)+1; operand2 = random.nextInt(operand1 + 1); exercises.append(operand1).append(" - ").append(operand2).append(" =\n"); break; case "100以内乘法": operand1 = random.nextInt(100)+1; operand2 = random.nextInt(operand1 + 1); exercises.append(operand1).append(" * ").append(operand2).append(" =\n"); break; case "100以内除法": operand1 = random.nextInt(100)+1; operand2 = random.nextInt(operand1 + 1); while(operand1%operand2!=0) { operand2 = random.nextInt(operand1 + 1); } exercises.append(operand1).append(" / ").append(operand2).append(" =\n"); break; } } exerciseArea.setText(exercises.toString()); resultArea.setText(""); answerField.setText(""); } private void checkAnswers() { String[] answers = answerField.getText().split(" "); String[] exercises = exerciseArea.getText().split("\n"); StringBuilder results = new StringBuilder(); System.out.println(answers.length); System.out.println(exercises.length); if (answers.length != exercises.length) { JOptionPane.showMessageDialog(this, "请回答所有题目。", "提示", JOptionPane.INFORMATION_MESSAGE); return; } for (int i = 0; i < exercises.length; i++) { String userAnswer = answers[i].trim(); String[] parts = exercises[i].split("\\s+"); int operand1 = Integer.parseInt(parts[0]); int operand2 = Integer.parseInt(parts[2]); int correctAnswer = 0; switch (parts[1]) { case "+": correctAnswer = operand1 + operand2; break; case "-": correctAnswer = operand1 - operand2; break; case "*": correctAnswer = operand1 * operand2; break; case "/": correctAnswer = operand1 / operand2; break; } boolean isCorrect = userAnswer.equals(String.valueOf(correctAnswer)); results.append(isCorrect ? "√" : "╳").append("\n"); } resultArea.setText(results.toString()); } private void showAnswers() { String[] exercises = exerciseArea.getText().split("\n"); StringBuilder answers = new StringBuilder(); for (String exercise : exercises) { String[] parts = exercise.split("\\s+"); int operand1 = Integer.parseInt(parts[0]); int operand2 = Integer.parseInt(parts[2]); int correctAnswer = 0; switch (parts[1]) { case "+": correctAnswer = operand1 + operand2; break; case "-": correctAnswer = operand1 - operand2; break; case "*": correctAnswer = operand1 * operand2; break; case "/": correctAnswer = operand1 / operand2; break; } answers.append(correctAnswer).append("\n"); } resultArea.setText("答案:\n" + answers.toString()); } }
用于以图形化界面的方式展示题目。