结对编程1(201421123065,201421123068)

码市地址:
https://coding.net/u/xmcyh1996/p/java/git/blob/master/sizeyunsuan.java

a. 需求分析
(1)基础需求
1.用户输入数量随机产生四则运算式子
2.对用户输入的答案进行正确率分析
3.GUI界面
(2)新功能需求
1.为了实现程序退出再启动还保存以前的对错数量,可以用一个txt文件来存储对错数量,并且根据每次生成时更新
2.在生成和答完题的按钮上用计时函数监控,算时间差
3.界面支持多语言,没有往复杂的方向想,因为只需要提供三种语言,可以通过按钮跳转,按钮里面修改标签的内容即可
(本次实现两个新功能为2和3)
(1.在开发的时候遇到了输入txt文件中产生乱码的情况,两个人一起排查无果…,在第三次commit中保留体现了1.的编程代码)
(3)扩展需求
界面人性化,引导用户便捷使用(例如生成框禁止用户输入,如果输入n不为数字,则提示用户)

b. 程序设计

c. 代码展示:展示每个功能的核心代码。

①点击“生成”按钮时

private void startActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    String num = enter1.getText();
    int n = 0;
    try {
        n = Integer.parseInt(num);
    } catch (NumberFormatException e) {
        if (language == 1) {
            resultprintf.setText("输入错误!请重新输入");
        } else if (language == 2) {
            resultprintf.setText("輸入錯誤!請重新輸入");
        } else if (language == 3) {
            resultprintf.setText("input error! please enter again");
        }
    }
    int m = (int) (Math.random() * n);//随机整数题目和分数题目的题量
    for (int i = 0; i < (n - m); i++) {//先随机出整数题型
        Random random = new Random();
        int n1 = random.nextInt(10);
        int n2 = random.nextInt(10) + 1;
        int a = (int) (Math.random() * 4 + 1);//随机决定运算类型
        if (a == 1) {
            Question.add(n1 + "+" + n2 + "=");
            Answer.add(n1 + n2 + "");
        }
        if (a == 2) {
            Question.add(n1 + "-" + n2 + "=");
            Answer.add(n1 - n2 + "");
        }
        if (a == 3) {
            Question.add(n1 + "×" + n2 + "=");
            Answer.add(n1 * n2 + "");
        }
        if (a == 4) {
            Question.add(n1 + "÷" + n2 + "=");
            String n3 = (float) n1 / n2 + "";
            if (n3.indexOf(".0") != -1) {
                n3 = n3.replace(".0", "");
            }
            Answer.add((n3));
        }
    }
    for (int i = 0; i < m; i++) {
        int[] fn1 = createFraction();
        int[] fn2 = createFraction();
        int a = (int) (Math.random() * 4 + 1);
        if (a == 1) {//加
            Question.add("(" + Reduction(fn1[0], fn1[1]) + ")+(" + Reduction(fn2[0], fn2[1]) + ")=");
            Answer.add(Reduction(((fn1[0] * fn2[1]) + (fn2[0] * fn1[1])), (fn1[1] * fn2[1])));//化简结果并存储
        }
        if (a == 2) {//减
            Question.add("(" + Reduction(fn1[0], fn1[1]) + ")-(" + Reduction(fn2[0], fn2[1]) + ")=");
            Answer.add(Reduction(((fn1[0] * fn2[1]) - (fn2[0] * fn1[1])), (fn1[1] * fn2[1])));
        }
        if (a == 3) {//乘
            Question.add("(" + Reduction(fn1[0], fn1[1]) + ")×(" + Reduction(fn2[0], fn2[1]) + ")=");
            Answer.add(Reduction(fn1[0] * fn2[0], fn1[1] * fn2[1]));//
        }
        if (a == 4) {//除
            Question.add("(" + Reduction(fn1[0], fn1[1]) + ")÷(" + Reduction(fn2[0], fn2[1]) + ")=");
            Answer.add(Reduction(fn1[0] * fn2[1], fn1[1] * fn2[0]));
        }
    }
    int qn = 0;
    question.setText("");
    rightanswer.setText("");
    enter2.setText("");
    for (String string : Question) {
        qn++;
        question.append("[" + qn + "]" + string + "
");
    }
    for (int i = 0; i < Question.size(); i++) {
        Question.remove(i);
    }
    String fromDate = simpleFormat.format(new Date());
    try {
        from = simpleFormat.parse(fromDate).getTime();
    } catch (ParseException ex) {
        Logger.getLogger(sizeyunsuan.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                     

②点击“做完”按钮时

private void finishActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    rightanswer.setText("");
    int correct = 0;
    int fault = 0;
    String[] anslist = enter2.getText().split("
");

    for (int i = 0; i < Answer.size(); i++) {
        if (Answer.get(i).equals(anslist[i])) {
            correct++;
            rightanswer.append(Answer.get(i) + "
");
        } else {
            rightanswer.append(Answer.get(i) + " ×
");
            fault++;
        }
    }

    String toDate = simpleFormat.format(new Date());
    try {
        to = simpleFormat.parse(toDate).getTime();
    } catch (ParseException ex) {
        Logger.getLogger(sizeyunsuan.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println((float) ((to - from) / (1000 * 60 * 60)));
    if (language == 1) {
        resultprintf.setText("答对" + correct + "题,正确率:" + (float) correct / (correct + fault) * 100 + "%,花费时间:" + (int) ((to - from) / (1000)) + "秒");
    } else if (language == 2) {
        resultprintf.setText("答對" + correct + "題,正確率:" + (float) correct / (correct + fault) * 100 + "%,花費時間:" + (int) ((to - from) / (1000)) + "秒");
    } else if (language == 3) {
        resultprintf.setText("Answer correct " + correct + "questions, correct rate:" + (float) correct / (correct + fault) * 100 + "%,Spend time:" + (int) ((to - from) / (1000)) + "s");
    }
    for (int i = 0; i < Answer.size(); i++) {
        Answer.remove(i);
        anslist = null;
    }
}                                      

③语言转换(以英文为例)

private void language3ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    resultprintf.setText("The current language is English");
    language = 3;
    title1.setText("Arithmetic");
    title2.setText("enter number:");
    title3.setText("language");
    title4.setText("problem display:");
    title5.setText("Enter answer:");
    title6.setText("proofreader:");
    start.setText("enter");
    finish.setText("done");
}                               

①简体中文界面

②繁体中文界面

③英语界面

e.描述结对的过程。
1.首先两个人讨论了一下关于新功能开发的想法,并初步制定解决方案
2.我对之前写的java代码移植到GUI,并且将界面定好,commit
3.队友对四则运算的基础功能进行优化并测试排查BUG,commit
4.处理一些在GUI界面上产生的新问题,例如如何对JtextArea情况
5.两个人分配写新功能123,commit
6.最后分别测试程序,注意代码规范,commit

f.PSP

g. 小结感受
1.结对编程的效果是很显著的,一个人编程容易走入思维的定式,总觉得自己的思路是没有错的,而两个人结对编程排除的速度是蹭蹭蹭的,效率明显提高。
2.当然也有一些我们俩也看不出来的问题,这时候请教一下其他同学也是收货满满的,在不断的排错中学习到了不少的东西。
3.产生了一些新的问题
比如随机生成式子的数量与用户输入的数量不一致(第二次生成开始)
两个人一起研究无果,向老师指教。
本来也写了累加用户对错题数,但是文件导入与输出乱码,不是太明白为什么?
4.一定程度上GUI界面会比命令行界面更友好
5.感谢队友对我不离不弃
6.最后谢谢老师的检查,老师辛苦了!

原文地址:https://www.cnblogs.com/xmwj/p/6551288.html