Java Swing 小学生算术练习系统(实验4必做作业)
Java Swing 图形界面实验小学生算术刷题系统完整源码一、实验要求二、实验环境操作系统Windows10/11IDEIntelliJ IDEAJDK 版本17开发技术Java Swing、ActionListener 事件监听、Random 随机数三、整体功能介绍难度分级简单10 以内加减法中等100 以内四则困难小数四则运算交互逻辑点击「下一题」刷新算式输入数字后点击「判断答案」弹窗提示正确 / 错误实时统计总答题、答对数量。界面布局使用FlowLayout流式布局组件不会遮挡窗口大小固定排版整洁。四、完整可运行源码包名jjj文件名MathPractice.javapackagejjj;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.Random;publicclassMathPracticeextendsJFrame{privateJRadioButtoneasyBtn,midBtn,hardBtn;privateJLabelquestionLabel;privateJTextFieldanswerInput;privateJButtonjudgeBtn,nextBtn;privateJLabelcountLabel;privateRandomrandomnewRandom();privateStringnowQuestion;privatedoublerightAnswer;privateinttotal0;privateintcorrect0;publicMathPractice(){setTitle(小学生算术练习系统);setSize(550,380);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);setLayout(newFlowLayout(FlowLayout.CENTER,15,15));setResizable(false);// 难度单选框add(newJLabel(选择难度));easyBtnnewJRadioButton(简单(10以内加减),true);midBtnnewJRadioButton(中等(100以内四则));hardBtnnewJRadioButton(困难(小数四则));ButtonGroupgroupnewButtonGroup();group.add(easyBtn);group.add(midBtn);group.add(hardBtn);add(easyBtn);add(midBtn);add(hardBtn);// 题目展示大字标签questionLabelnewJLabel(点击下一题生成题目);questionLabel.setFont(newFont(宋体,Font.BOLD,28));add(questionLabel);// 答案输入区域add(newJLabel(你的答案));answerInputnewJTextField(12);judgeBtnnewJButton(判断答案);nextBtnnewJButton(下一题);add(answerInput);add(judgeBtn);add(nextBtn);// 答题统计countLabelnewJLabel(总答题0 答对0);countLabel.setFont(newFont(宋体,Font.PLAIN,16));add(countLabel);// 绑定按钮点击事件nextBtn.addActionListener(newNextQuestionListener());judgeBtn.addActionListener(newJudgeAnswerListener());}// 根据难度生成随机题目privatevoidcreateQuestion(){intop1,op2;charop;if(easyBtn.isSelected()){op1random.nextInt(10)1;op2random.nextInt(10)1;oprandom.nextBoolean()?:-;if(op-op1op2){inttempop1;op1op2;op2temp;}rightAnswerop?op1op2:op1-op2;}elseif(midBtn.isSelected()){op1random.nextInt(100)1;op2random.nextInt(20)1;char[]ops{,-,*,/};opops[random.nextInt(4)];if(op-op1op2){inttempop1;op1op2;op2temp;}switch(op){case:rightAnswerop1op2;break;case-:rightAnswerop1-op2;break;case*:rightAnswerop1*op2;break;case/:rightAnswer(double)op1/op2;break;}}else{doubled1Math.round(random.nextDouble()*100*10)/10.0;doubled2Math.round(random.nextDouble()*20*10)/10.0;char[]ops{,-,*,/};opops[random.nextInt(4)];switch(op){case:rightAnswerd1d2;break;case-:rightAnswerd1-d2;break;case*:rightAnswerd1*d2;break;case/:rightAnswerd1/d2;break;}nowQuestiond1 op d2 ?;questionLabel.setText(题目nowQuestion);return;}nowQuestionop1 op op2 ?;questionLabel.setText(题目nowQuestion);}// 下一题按钮监听类classNextQuestionListenerimplementsActionListener{OverridepublicvoidactionPerformed(ActionEvente){answerInput.setText();createQuestion();}}// 判断答案按钮监听类classJudgeAnswerListenerimplementsActionListener{OverridepublicvoidactionPerformed(ActionEvente){StringinputStranswerInput.getText().trim();if(input.isEmpty()){JOptionPane.showMessageDialog(null,请输入答案);return;}doubleuserAns;try{userAnsDouble.parseDouble(inputStr);}catch(Exceptionex){JOptionPane.showMessageDialog(null,输入格式错误请输入数字);return;}total;if(Math.abs(userAns-rightAnswer)0.01){correct;JOptionPane.showMessageDialog(null,回答正确太棒了);}else{JOptionPane.showMessageDialog(null,回答错误正确答案是String.format(%.2f,rightAnswer));}countLabel.setText(总答题total 答对correct);}}publicstaticvoidmain(String[]args){MathPracticeframenewMathPractice();frame.setVisible(true);}}五、运行效果截图六、实验总结本次实验掌握 Swing 基础组件与事件处理机制理解事件源、监听器的绑定流程。通过分层难度设计、随机数生成算式练习了分支判断与浮点数运算。开发中遇到组件遮挡、输入异常等问题通过调整布局、增加异常捕获解决。完整实现题目刷新、答案校验、数据统计功能巩固面向对象 GUI 开发思路为后续复杂图形程序打下基础。七、心得体会本次算术系统开发让我从零熟悉 Java 可视化编程区别于控制台纯文本程序。初期因布局选择不当出现组件错位更换流式布局后界面显示稳定。事件监听是核心知识点两个按钮分别绑定独立监听器实现刷新、校验两套逻辑。同时处理空输入、非数字输入异常提升程序健壮性。三层难度区分锻炼分支控制能力整数与小数兼容计算拓宽数值处理思路。整个项目代码分层清晰功能模块化便于修改拓展。调试过程培养了分步排查 bug 的习惯深刻理解 GUI 程序 “界面 逻辑分离” 的开发思想后续可以新增分数运算、错题保存等拓展功能