天天看點

結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器

合作夥伴 201421123089 周迪 201421123069 黃睿

代碼位址:https://git.coding.net/H1159650478/sizeyunsuanjiaqiang.git

題目描述

我們在個人作業1中,用各種語言實作了一個指令行的四則運算小程式。進一步,本次要求把這個程式做成GUI(可以是Windows PC 上的,也可以是Mac、Linux,web,手機上的),成為一個有基本功能、一定價值的程式。在下面的功能需求中實作兩個:

記錄使用者的對錯總數,程式退出再啟動的時候,能把以前的對錯數量儲存并在此基礎上增量計算。

有計時功能,能顯示使用者開始答題後的消耗時間。

界面支援中文簡體/中文繁體/英語,使用者可以選擇一種。

實作功能:

1、題目數量選擇;

2、難度等級選擇;

3、記錄使用者的對錯總數,程式退出再啟動的時候,能把以前的對錯數量儲存并在此基礎上增量計算;

4、有計時功能,能顯示使用者開始答題後的消耗時間;

5、限定使用者輸入(不允許非數字)

思維導圖

題目生成與計算方法

結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器
結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器

關鍵代碼

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("初級or進階?");
             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);
         }
      

  

實作功能截圖

1

結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器

2

結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器

3

結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器

4

結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器

5

結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器

實驗總結

1、對于本次結對程式設計是否真的能夠帶來1+1>2的效果,我現在有了深刻的體會,兩個人一起程式設計可以互相激勵、共同進步;

2、在結對程式設計中會發現隊友在程式設計中的許多好的習慣和方法,也提高了自己的程式設計能力;

3、結對程式設計更适用于解決方向性的問題;

4、結對程式設計中雙方的互動可以利于思維的開啟,收獲頗多。

結對展示

結對程式設計1 —— 基于GUI和Swing的四則運算題目生成器

PSP展示

PSP2.1 Personal Software Process Stages Time Senior Student Time
Planning 計劃 6
· Estimate 估計這個任務需要多少時間 7
Development 開發 80 77
· Analysis 需求分析 (包括學習新技術)
· Design Spec 生成設計文檔
· Design Review 設計複審 9
· Coding Standard 代碼規範
· Design 具體設計 23 24
· Coding 具體編碼 35 30
· Code Review 代碼複審
· Test 測試(自我測試,修改代碼,送出修改) 15 25
Reporting 報告 10
· 測試報告
計算工作量
并提出過程改進計劃
上一篇: 繼承和多态