当前位置: 首页 > news >正文

小学算术题

设计并完成一个能运行的且界面美观的小软件。提交可运行软件
程序主要针对小学生的算术计算。
1、可以自定义计算的难度(此项可根据功能进行扩展)
2、随机获取不一样的题目,能通过按键触发确定填写输入的答案是否正确。
3、计算满足+ - * /(可扩展)
4、操作数可以是整数、小数、分数等等(可扩展)

importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.util.Random;publicclassArithmeticQuizextendsJFrame{privateJComboBox<String>operationCombo;// 现在存储 "+", "-", "*", "/"privateJTextFieldminField,maxField;privateJCheckBoxdecimalCheck;privateJTextFielddecimalPlacesField;privateJButtongenerateBtn,checkBtn,nextBtn;privateJLabelquestionLabel,feedbackLabel,scoreLabel;privateJTextFieldanswerField;privatedoublecurrentAnswer;privateStringcurrentQuestion;privateinttotalCount=0;privateintcorrectCount=0;privateRandomrandom=newRandom();publicArithmeticQuiz(){setTitle("🧮 小学生算术练习");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(600,400);setLocationRelativeTo(null);setResizable(true);setMinimumSize(newDimension(450,350));JPanelmainPanel=newJPanel(newBorderLayout(10,10));mainPanel.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));// 控制面板JPanelcontrolPanel=newJPanel(newGridLayout(3,4,8,8));controlPanel.setBorder(BorderFactory.createTitledBorder("⚙ 难度设置"));controlPanel.add(newJLabel("运算:"));// --- 修改点:直接使用符号,不再带中文 ---operationCombo=newJComboBox<>(newString[]{"+","-","*","/"});controlPanel.add(operationCombo);controlPanel.add(newJLabel("最小值:"));minField=newJTextField("1",5);controlPanel.add(minField);controlPanel.add(newJLabel("最大值:"));maxField=newJTextField("10",5);controlPanel.add(maxField);controlPanel.add(newJLabel("包含小数:"));decimalCheck=newJCheckBox();decimalCheck.setSelected(false);controlPanel.add(decimalCheck);controlPanel.add(newJLabel("小数位数:"));decimalPlacesField=newJTextField("1",3);decimalPlacesField.setEnabled(false);controlPanel.add(decimalPlacesField);generateBtn=newJButton("生成新题目");controlPanel.add(generateBtn);mainPanel.add(controlPanel,BorderLayout.NORTH);// 中央面板JPanelcenterPanel=newJPanel(newGridBagLayout());centerPanel.setBorder(BorderFactory.createTitledBorder("📝 题目"));GridBagConstraintsgbc=newGridBagConstraints();gbc.insets=newInsets(5,5,5,5);gbc.gridx=0;gbc.gridy=0;gbc.gridwidth=2;questionLabel=newJLabel("请点击“生成新题目”开始",SwingConstants.CENTER);questionLabel.setFont(newFont("宋体",Font.BOLD,24));questionLabel.setForeground(Color.BLUE);centerPanel.add(questionLabel,gbc);gbc.gridy=1;gbc.gridwidth=1;gbc.gridx=0;centerPanel.add(newJLabel("你的答案: "),gbc);gbc.gridx=1;answerField=newJTextField(10);answerField.setFont(newFont("宋体",Font.PLAIN,18));answerField.setEditable(true);answerField.setEnabled(true);centerPanel.add(answerField,gbc);gbc.gridy=2;gbc.gridx=0;checkBtn=newJButton("检查答案");centerPanel.add(checkBtn,gbc);gbc.gridx=1;nextBtn=newJButton("下一题");centerPanel.add(nextBtn,gbc);mainPanel.add(centerPanel,BorderLayout.CENTER);// 底部面板JPanelbottomPanel=newJPanel(newGridLayout(2,1,5,5));bottomPanel.setBorder(BorderFactory.createTitledBorder("📊 反馈与统计"));feedbackLabel=newJLabel(" ",SwingConstants.CENTER);feedbackLabel.setFont(newFont("宋体",Font.PLAIN,16));bottomPanel.add(feedbackLabel);scoreLabel=newJLabel("答题总数: 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{intmin=Integer.parseInt(minField.getText().trim());intmax=Integer.parseInt(maxField.getText().trim());if(min>max){JOptionPane.showMessageDialog(this,"最小值不能大于最大值!","输入错误",JOptionPane.ERROR_MESSAGE);return;}booleanhasDecimal=decimalCheck.isSelected();intdecimalPlaces=1;if(hasDecimal){decimalPlaces=Integer.parseInt(decimalPlacesField.getText().trim());if(decimalPlaces<0)decimalPlaces=0;}// 直接获取符号(现在下拉框存的是 "+", "-", "*", "/")StringopStr=(String)operationCombo.getSelectedItem();charop=opStr.charAt(0);// 安全,因为只有一个字符doublenum1=generateNumber(min,max,hasDecimal,decimalPlaces);doublenum2=generateNumber(min,max,hasDecimal,decimalPlaces);if(op=='-'&&num1<num2){doubletemp=num1;num1=num2;num2=temp;}if(op=='/'&&num2==0)num2=1.0;Stringnum1Str=formatNumber(num1);Stringnum2Str=formatNumber(num2);currentQuestion=num1Str+" "+op+" "+num2Str+" = ?";questionLabel.setText(currentQuestion);doubleresult=0;switch(op){case'+':result=num1+num2;break;case'-':result=num1-num2;break;case'*':result=num1*num2;break;case'/':result=num1/num2;break;default:result=0;}if(hasDecimal){result=round(result,decimalPlaces);}else{result=Math.round(result);}currentAnswer=result;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){intintPart=min+random.nextInt(max-min+1);doublefracPart=random.nextDouble();doublevalue=intPart+fracPart;returnround(value,decimalPlaces);}else{returnmin+random.nextInt(max-min+1);}}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(currentQuestion==null){JOptionPane.showMessageDialog(this,"请先生成一道题目!","提示",JOptionPane.INFORMATION_MESSAGE);return;}Stringinput=answerField.getText().trim();if(input.isEmpty()){JOptionPane.showMessageDialog(this,"请输入答案!","提示",JOptionPane.WARNING_MESSAGE);return;}try{doubleuserAnswer=Double.parseDouble(input);booleanisCorrect=Math.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(){doublerate=totalCount==0?0:(double)correctCount/totalCount*100;scoreLabel.setText(String.format("答题总数: %d 正确: %d 正确率: %.1f%%",totalCount,correctCount,rate));}publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(()->newArithmeticQuiz().setVisible(true));}}

http://www.jsqmd.com/news/1106095/

相关文章:

  • 用最新 GPT-5.6 润色论文是一种怎么样的体验?
  • 开源视频生成模型选择
  • SpringBoot+Vue 私人西服定制_leabo管理平台源码【适合毕设/课设/学习】Java+MySQL
  • 检索增强架构实践:家庭回忆录助手如何避免编造
  • 提示词 与 工作流 编排:复杂流程要拆成可观测节点
  • 炉石传说智能脚本:7倍效率提升的自动化神器
  • 多机位像素同源融合渲染,一套图形底座搭建无割裂全域数字世界
  • 终极自动化Gofile下载神器:告别繁琐手动操作
  • 一张图讲清楚:Codex上下文
  • SPARK技术:5G/6G无线通信中的辐射模式压缩革命
  • 分布式系统到 AI 创业:架构师转型 CEO 的三个误区
  • 3个步骤深度解析RTL8821CU驱动:完全解决Linux无线网卡兼容性问题
  • VMware虚拟机IP固化失败率高达63.8%?——基于127家企业的配置审计报告,给出唯一可审计、可回滚、可自动化部署方案
  • AI 数据分析落地:别让智能洞察变成自动废话机
  • 番茄小说下载器:三分钟构建你的个人数字图书馆,随时随地享受纯净阅读
  • Python 异步 检索增强:端到端延迟要按阶段拆开
  • 佳易王计时计费管理软件打印设置完整教程(含故障排查+远程批量打印)
  • 如何轻松实现跨平台输入法词库转换:深蓝词库转换工具完全指南
  • AI 辅助:高性能 RPC 框架设计:延迟预算要从协议层开始
  • AI 辅助:用生活化比喻讲统计:置信区间不是玄学范围
  • Go Channel 的运行时实现:环形队列、信号量与调度器协作
  • 2025了钉钉会议转任务还效率低?听脑真能一键解决吗?
  • 构建安全可靠的脑植入式医疗系统
  • 亮数据+Scraper studio实战
  • TensorFlow Lite Micro 优化:算子少一点,系统稳一点
  • 一、项目简介一个基于 C++ 的简易控制台计算器,支持多种基础运算。二、功能说明
  • AI 辅助:刷题系统:如何把题解生成变成可验证流程
  • 英语口语基础语法学习
  • 7.5k Star!仅7MB的AI终端,把IDE、Git和AI Agent全部装进一个窗口
  • CVPR 2026|AnyVisLoc:为真实低空无人机视觉定位建立统一基准