结对编程1 —— 基于GUI和Swing的四则运算题目生成器

合作伙伴 201421123102 王艳秋 201421123106 陈 雄

代码地址

题目描述

我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序。进一步,本次要求把这个程序做成GUI(可以是Windows PC 上的,也可以是Mac、Linux,web,手机上的),成为一个有基本功能、一定价值的程序。在下面的功能需求中实现两个:

记录用户的对错总数,程序退出再启动的时候,能把以前的对错数量保存并在此基础上增量计算。
有计时功能,能显示用户开始答题后的消耗时间。
界面支持中文简体/中文繁体/英语,用户可以选择一种。

实现功能:

1、题目数量选择;
2、难度等级选择;
3、记录用户的对错总数,程序退出再启动的时候,能把以前的对错数量保存并在此基础上增量计算;
4、有计时功能,能显示用户开始答题后的消耗时间;
5、多语言选择;
6、限定用户输入(不允许非数字)

结对分工

王艳秋(在下): 1、语言切换功能 2、代码规范

陈 雄(伙伴): 1、答题计时功能 2、对错总数功能

思维导图

1112338-20170314205514588-740516027.png
1113658-20170314224302838-1878756034.png

关键代码

public void panelRepaint(JPanel a,JPanel b,JPanel c){
             a.remove(b);
             a.add(c);
             a.revalidate();
             a.repaint();
      }

以testJFrame为顶级容器,panel为二级容器,panel下面七个面板作为三级容器放置各种组件。该函数实现了panel面板的重绘以便在各个三级面板之间切换。

if(e.getActionCommand().equals("sure")){
             this.questNum=quesNumJTA.getText();
             if(!questNum.matches("[0-9]*[1-9][0-9]*")){
                 panelRepaint(panel,quseNumJP,warningJP);
             }
             else{
                 panelRepaint(panel,quseNumJP,difficultyChooseJP);
             }
         }

程序中当用户输入题目数量时可能会出现输入的数为0,输入字符,输入小数,输入分数及输入负数的情况。在用户输入之后点击确定,这段代码会使用正则表达式对用户的输入进行匹配,若匹配不到大于0的整数界面将切换到warningJP提示用户重新输入。

if(e.getActionCommand().equals("easy")){
             startTime=System.currentTimeMillis();
             this.easyOrDifficult=1;
             pMaker.creatExecercise(easyOrDifficult);
             showQuestionJL.setText(pMaker.quesStr);
             panelRepaint(panel,difficultyChooseJP,answerJP);
         }
         if(e.getActionCommand().equals("difficult")){
             startTime=System.currentTimeMillis();
             this.easyOrDifficult=2;
             pMaker.creatExecercise(easyOrDifficult);
             showQuestionJL.setText(pMaker.quesStr);
             panelRepaint(panel,difficultyChooseJP,answerJP);
         }
         if(e.getActionCommand().equals("next")){
             if(i<Integer.parseInt(questNum)){
                 panelRepaint(panel,nextJP,answerJP);
                 i++;
             }
             else{
                endTime=System.currentTimeMillis();
                File file=new File("D:/count.txt");
                BufferedReader reader=null;
                try {
                    reader=new BufferedReader(new FileReader(file));
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                String i=null;
                String i1=null;
                try {
                    i=reader.readLine();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    i1=reader.readLine();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                PrintWriter writer=null;
                try {
                    writer =new PrintWriter(new FileOutputStream(file));
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                writer.println((Integer.parseInt(i)+trueCount));
                writer.println((Integer.parseInt(i1)+(Integer.parseInt(questNum)-trueCount)));
                writer.flush();
                try {
                    reader=new BufferedReader(new FileReader(file));
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                try {
                    i=reader.readLine();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    i1=reader.readLine();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                if(chineseOrEnglish==0){
                    showCountJL.setText("正确题数:"+trueCount+" 错误题数:"+(Integer.parseInt(questNum)-trueCount)+" 用时: "+((endTime-startTime)/1000)+" s"+" 历史总正确题数: "+i+" 历史总错误题数: "+i1);
                }
                else{
                    showCountJL.setText("true:"+trueCount+" false:"+(Integer.parseInt(questNum)-trueCount)+" time: "+((endTime-startTime)/1000)+" s"+" totaltrue: "+i+" totalfalse: "+i1);
                }
                panelRepaint(panel,nextJP,countJP);
             }
         }

这段代码实现了计时功能,用户选择完难度后计录系统时间作为开始时间,做完最后一题后用户按next记录系统时间作为结束时间。结束时间减去开始时间为做题时间。同时实现了对历史记录的写入读出功能。在D盘创建里count.txt文件第一行作为总的做对的题数,第二行作为总错题数。执行时先读入文件记录在将本次记录加上并写入。
最后读出并显示。

if(e.getActionCommand().equals("chinese")){
             chineseOrEnglish=0;
             numQuizJL.setText("你想测试多少题?");
             difficultyQuizJL.setText("你想测试的难易程度?");
             warningJL.setText("请输入一个大于0的整数!!");
             sureJB.setText("确定");
             easyJB.setText("容易");
             difficultJB.setText("难");
             nextJB.setText("下一题");
             submitJB.setText("提交");
             reenterJB.setText("重新输入");
             panelRepaint(panel,languageChoJP,quseNumJP);
         }
         if(e.getActionCommand().equals("english")){
             chineseOrEnglish=1;
             numQuizJL.setText("how many questions u want test?");
             difficultyQuizJL.setText("easy or difficult?");
             warningJL.setText("please enter a number greater than 0!!");
             sureJB.setText("sure");
             easyJB.setText("easy");
             difficultJB.setText("difficult");
             nextJB.setText("next");
             submitJB.setText("subnit");
             reenterJB.setText("reenter");
             panelRepaint(panel,languageChoJP,quseNumJP);
         }

该代码实现了用户界面的中英文切换。一些特殊组件在显示时用if条件判断须显示中文还是英文。

实现功能截图

1112338-20170314211049620-749589366.png
语言选择

中文版

1112338-20170314211106729-270975803.png
选择题目数量
1112338-20170314211220354-575650809.png
当输入0或非数字时的警告
1112338-20170314211307526-654356282.png
难度选择
1112338-20170314211349198-982743633.png
题目生成
1112338-20170314211414041-1057548165.png
回答正确
1112338-20170314211448807-1880840097.png
题目生成
1112338-20170314211532385-906166581.png
回答错误,并给出正确答案
1112338-20170314211600635-365834196.png
答题结束,显示结果

英文版

1112338-20170314211703057-561842582.png
选择题目数量
1112338-20170314211845776-1960240512.png
当输入0或非数字时的警告
1112338-20170314212611620-695077518.png
难度选择
1112338-20170314211904104-659301176.png
题目生成
1112338-20170314212037479-688246493.png
回答正确
1112338-20170314212059010-1647233789.png
回答错误,并给出正确答案
1112338-20170314212219541-204018961.png
答题结束,显示结果

实验总结

1、结对编程中,伙伴总是能不厌其烦地为我解除疑惑,互帮互助,一起进步;
2、在结对编程中就算自己不想继续下去时,也会因为伙伴而坚持下去;
3、在上一次的作业中我没能好好的规范代码,在这一次的试验中我也深深的体会到了代码规范的重要性。

结对展示

1112338-20170314213934448-154227883.jpg
1112338-20170314213952479-1925265659.jpg

PSP展示

PSP2.1Personal Software Process StagesTime (%) Senior Student(/hour)Time (%)(/hour)
· Planning计划21.5
· Estimate估计这个任务需要多少时间4152
· Analysis需求分析 (包括学习新技术)35
· Coding Standard代码规范0.51.5
· Design具体设计1.53
· Coding具体编码3035
· Test测试(自我测试,修改代码,提交修改)23
Reporting报告23

转载于:https://www.cnblogs.com/yuzhouwudimeishaonv7/p/6551109.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值