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

合作伙伴 201421123106 陈 雄 201421123102 王艳秋

代码地址

题目描述

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

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

实现功能:

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

结对分工

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

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

思维导图

题目生成与计算方法

GUI框架

关键代码

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("count");
     			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条件判断须显示中文还是英文。

实现功能截图


语言选择

中文版


选择题目数量

当输入0或非数字时的警告

难度选择

题目生成

回答正确

题目生成

回答错误,并给出正确答案

答题结束,显示结果

英文版


选择题目数量

当输入0或非数字时的警告

难度选择

题目生成

回答正确

回答错误,并给出正确答案

答题结束,显示结果

实验总结

1、对于本次结对编程是否真的能够带来1+1>2的效果,我现在有了深刻的体会,两个人一起编程可以相互激励、共同进步;
2、在结对编程中会发现队友在编程中的许多好的习惯和方法,也提高了自己的编程能力;
3、结对编程更适用于解决方向性的问题;
4、结对编程中双方的互动可以利于思维的开启,收获颇多。

结对展示



PSP展示

PSP2.1 Personal Software Process Stages Time (%) Senior Student(/hour) Time (%)(/hour)
· Planning 计划 2 1.5
· Estimate 估计这个任务需要多少时间 41 52
· Analysis 需求分析 (包括学习新技术) 3 5
· Coding Standard 代码规范 0.5 1.5
· Design 具体设计 1.5 3
· Coding 具体编码 30 35
· Test 测试(自我测试,修改代码,提交修改) 2 3
Reporting 报告 2 3
posted @ 2017-03-14 22:43  陈大熊  阅读(412)  评论(6编辑  收藏  举报