
设计并完成一个能运行的且界面美观的小软件。提交可运行软件程序主要针对小学生的算术计算。1、可以自定义计算的难度此项可根据功能进行扩展2、随机获取不一样的题目能通过按键触发确定填写输入的答案是否正确。3、计算满足 - * /(可扩展4、操作数可以是整数、小数、分数等等可扩展importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.util.Random;publicclassArithmeticQuizextendsJFrame{privateJComboBoxStringoperationCombo;// 现在存储 , -, *, /privateJTextFieldminField,maxField;privateJCheckBoxdecimalCheck;privateJTextFielddecimalPlacesField;privateJButtongenerateBtn,checkBtn,nextBtn;privateJLabelquestionLabel,feedbackLabel,scoreLabel;privateJTextFieldanswerField;privatedoublecurrentAnswer;privateStringcurrentQuestion;privateinttotalCount0;privateintcorrectCount0;privateRandomrandomnewRandom();publicArithmeticQuiz(){setTitle( 小学生算术练习);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(600,400);setLocationRelativeTo(null);setResizable(true);setMinimumSize(newDimension(450,350));JPanelmainPanelnewJPanel(newBorderLayout(10,10));mainPanel.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));// 控制面板JPanelcontrolPanelnewJPanel(newGridLayout(3,4,8,8));controlPanel.setBorder(BorderFactory.createTitledBorder(⚙ 难度设置));controlPanel.add(newJLabel(运算:));// --- 修改点直接使用符号不再带中文 ---operationCombonewJComboBox(newString[]{,-,*,/});controlPanel.add(operationCombo);controlPanel.add(newJLabel(最小值:));minFieldnewJTextField(1,5);controlPanel.add(minField);controlPanel.add(newJLabel(最大值:));maxFieldnewJTextField(10,5);controlPanel.add(maxField);controlPanel.add(newJLabel(包含小数:));decimalChecknewJCheckBox();decimalCheck.setSelected(false);controlPanel.add(decimalCheck);controlPanel.add(newJLabel(小数位数:));decimalPlacesFieldnewJTextField(1,3);decimalPlacesField.setEnabled(false);controlPanel.add(decimalPlacesField);generateBtnnewJButton(生成新题目);controlPanel.add(generateBtn);mainPanel.add(controlPanel,BorderLayout.NORTH);// 中央面板JPanelcenterPanelnewJPanel(newGridBagLayout());centerPanel.setBorder(BorderFactory.createTitledBorder( 题目));GridBagConstraintsgbcnewGridBagConstraints();gbc.insetsnewInsets(5,5,5,5);gbc.gridx0;gbc.gridy0;gbc.gridwidth2;questionLabelnewJLabel(请点击“生成新题目”开始,SwingConstants.CENTER);questionLabel.setFont(newFont(宋体,Font.BOLD,24));questionLabel.setForeground(Color.BLUE);centerPanel.add(questionLabel,gbc);gbc.gridy1;gbc.gridwidth1;gbc.gridx0;centerPanel.add(newJLabel(你的答案: ),gbc);gbc.gridx1;answerFieldnewJTextField(10);answerField.setFont(newFont(宋体,Font.PLAIN,18));answerField.setEditable(true);answerField.setEnabled(true);centerPanel.add(answerField,gbc);gbc.gridy2;gbc.gridx0;checkBtnnewJButton(检查答案);centerPanel.add(checkBtn,gbc);gbc.gridx1;nextBtnnewJButton(下一题);centerPanel.add(nextBtn,gbc);mainPanel.add(centerPanel,BorderLayout.CENTER);// 底部面板JPanelbottomPanelnewJPanel(newGridLayout(2,1,5,5));bottomPanel.setBorder(BorderFactory.createTitledBorder( 反馈与统计));feedbackLabelnewJLabel( ,SwingConstants.CENTER);feedbackLabel.setFont(newFont(宋体,Font.PLAIN,16));bottomPanel.add(feedbackLabel);scoreLabelnewJLabel(答题总数: 0 正确: 0 正确率: 0%,SwingConstants.CENTER);scoreLabel.setFont(newFont(宋体,Font.PLAIN,14));bottomPanel.add(scoreLabel);mainPanel.add(bottomPanel,BorderLayout.SOUTH);add(mainPanel);// 事件监听decimalCheck.addActionListener(e-{decimalPlacesField.setEnabled(decimalCheck.isSelected());if(!decimalCheck.isSelected())decimalPlacesField.setText(1);});generateBtn.addActionListener(e-generateQuestion());checkBtn.addActionListener(e-checkAnswer());nextBtn.addActionListener(e-generateQuestion());answerField.addActionListener(e-checkAnswer());addWindowListener(newWindowAdapter(){OverridepublicvoidwindowOpened(WindowEvente){answerField.requestFocusInWindow();}});}privatevoidgenerateQuestion(){try{intminInteger.parseInt(minField.getText().trim());intmaxInteger.parseInt(maxField.getText().trim());if(minmax){JOptionPane.showMessageDialog(this,最小值不能大于最大值,输入错误,JOptionPane.ERROR_MESSAGE);return;}booleanhasDecimaldecimalCheck.isSelected();intdecimalPlaces1;if(hasDecimal){decimalPlacesInteger.parseInt(decimalPlacesField.getText().trim());if(decimalPlaces0)decimalPlaces0;}// 直接获取符号现在下拉框存的是 , -, *, /StringopStr(String)operationCombo.getSelectedItem();charopopStr.charAt(0);// 安全因为只有一个字符doublenum1generateNumber(min,max,hasDecimal,decimalPlaces);doublenum2generateNumber(min,max,hasDecimal,decimalPlaces);if(op-num1num2){doubletempnum1;num1num2;num2temp;}if(op/num20)num21.0;Stringnum1StrformatNumber(num1);Stringnum2StrformatNumber(num2);currentQuestionnum1Str op num2Str ?;questionLabel.setText(currentQuestion);doubleresult0;switch(op){case:resultnum1num2;break;case-:resultnum1-num2;break;case*:resultnum1*num2;break;case/:resultnum1/num2;break;default:result0;}if(hasDecimal){resultround(result,decimalPlaces);}else{resultMath.round(result);}currentAnswerresult;answerField.setText();feedbackLabel.setText(请输入答案然后点击“检查”);feedbackLabel.setForeground(Color.BLACK);answerField.requestFocusInWindow();}catch(NumberFormatExceptionex){JOptionPane.showMessageDialog(this,请输入有效的数字,输入错误,JOptionPane.ERROR_MESSAGE);}}privatedoublegenerateNumber(intmin,intmax,booleandecimal,intdecimalPlaces){if(decimal){intintPartminrandom.nextInt(max-min1);doublefracPartrandom.nextDouble();doublevalueintPartfracPart;returnround(value,decimalPlaces);}else{returnminrandom.nextInt(max-min1);}}privateStringformatNumber(doublenum){if(num(long)num)returnString.valueOf((long)num);elsereturnString.valueOf(num);}privatedoubleround(doublevalue,intplaces){longfactor(long)Math.pow(10,places);returnMath.round(value*factor)/(double)factor;}privatevoidcheckAnswer(){if(currentQuestionnull){JOptionPane.showMessageDialog(this,请先生成一道题目,提示,JOptionPane.INFORMATION_MESSAGE);return;}StringinputanswerField.getText().trim();if(input.isEmpty()){JOptionPane.showMessageDialog(this,请输入答案,提示,JOptionPane.WARNING_MESSAGE);return;}try{doubleuserAnswerDouble.parseDouble(input);booleanisCorrectMath.abs(userAnswer-currentAnswer)0.0001;totalCount;if(isCorrect){correctCount;feedbackLabel.setText(✅ 回答正确 正确答案是 formatNumber(currentAnswer));feedbackLabel.setForeground(Color.GREEN);}else{feedbackLabel.setText(❌ 回答错误。正确答案是 formatNumber(currentAnswer));feedbackLabel.setForeground(Color.RED);}updateScore();}catch(NumberFormatExceptionex){JOptionPane.showMessageDialog(this,请输入有效的数字,输入错误,JOptionPane.ERROR_MESSAGE);}}privatevoidupdateScore(){doubleratetotalCount0?0:(double)correctCount/totalCount*100;scoreLabel.setText(String.format(答题总数: %d 正确: %d 正确率: %.1f%%,totalCount,correctCount,rate));}publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(()-newArithmeticQuiz().setVisible(true));}}