zoukankan      html  css  js  c++  java
  • (第四周)四则运算单元测试

    项目成员:黄兴、谢孝淼

    程序代码

      因为此前的程序只有一个主函数main(),为了做单元测试,把代码进行了一些修改,把源代码分到了几个方法里。

    1、生成题目并计算的addQuestion()方法,其中把随机数和随机运算符都设成了固定值,这样是为了更容易的进行单元测试,除了这个方式,还没有想好怎样对随机数的方法进行测  Scanner sc = new Scanner(System.in); //创建Scanner类的对象

        double result=0;
        int right=0;                           //统计正确题目的数量
        public double addQuestion(){
            int a = 1;
            int b = 2;
            int c = 3;
            int d = 4;
    int i = 1; int j = 2; int k = 3; char chrs[] = { '.','+', '-', '*', '/' }; //将运算符号存到数组里 String.valueOf(chrs[i]); String.valueOf(chrs[j]); String.valueOf(chrs[k]); //用switch,case对三个运算符进行判断,方法比较笨,列出了全部64种情况 switch(i){ case 1:{ switch(j){ case 1:{ switch(k){ case 1:System.out.println(a+"+"+b+"+"+c+"+"+d+"=?"); result=a+b+c+d; break; case 2:System.out.println(a+"+"+b+"+"+c+"-"+d+"=?"); result=a+b+c-d; break; case 3:System.out.println(a+"+"+b+"+"+c+"*"+d+"=?"); result=a+b+c*d; break; case 4:System.out.println(a+"+"+b+"+"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a+b+f/g; } break; } } break; case 2:{ switch(k){ case 1:System.out.println(a+"+"+b+"-"+c+"+"+d+"=?"); result=a+b-c+d; break; case 2:System.out.println(a+"+"+b+"-"+c+"-"+d+"=?"); result=a+b-c-d; break; case 3:System.out.println(a+"+"+b+"-"+c+"*"+d+"=?"); result=a+b-c*d; break; case 4:System.out.println(a+"+"+b+"-"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a+b-f/g; } break; } } break; case 3:{ switch(k){ case 1:System.out.println(a+"+"+b+"*"+c+"+"+d+"=?"); result=a+b*c+d; break; case 2:System.out.println(a+"+"+b+"*"+c+"-"+d+"=?"); result=a+b*c-d; break; case 3:System.out.println(a+"+"+b+"*"+c+"*"+d+"=?"); result=a+b*c*d; break; case 4:System.out.println(a+"+"+b+"*"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a+b*f/g; } break; } } break; case 4:{ switch(k){ case 1:System.out.println(a+"+"+b+"/"+c+"+"+d+"=?"); float f1 = b; float g1 = c; if(g1!=0){ result =a+f1/g1+d; } break; case 2:System.out.println(a+"+"+b+"/"+c+"-"+d+"=?"); float f2 = b; float g2 = c; if(g2!=0){ result =a+f2/g2-d; } break; case 3:System.out.println(a+"+"+b+"/"+c+"*"+d+"=?"); float f3 = b; float g3 = c; if(g3!=0){ result =a+f3/g3*d; } break; case 4:System.out.println(a+"+"+b+"/"+c+"/"+d+"=?"); float f4 = b; float g4 = c; float p4 = d; if(g4!=0 && p4!=0){ result =a+f4/g4/p4; } break; } } break; } } break; case 2:{ switch(j){ case 1:{ switch(k){ case 1:System.out.println(a+"-"+b+"+"+c+"+"+d+"=?"); result=a-b+c+d; break; case 2:System.out.println(a+"-"+b+"+"+c+"-"+d+"=?"); result=a-b+c-d; break; case 3:System.out.println(a+"-"+b+"+"+c+"*"+d+"=?"); result=a-b+c*d; break; case 4:System.out.println(a+"-"+b+"+"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a-b+f/g; } break; } } break; case 2:{ switch(k){ case 1:System.out.println(a+"-"+b+"-"+c+"+"+d+"=?"); result=a-b-c+d; break; case 2:System.out.println(a+"-"+b+"-"+c+"-"+d+"=?"); result=a-b-c-d; break; case 3:System.out.println(a+"-"+b+"-"+c+"*"+d+"=?"); result=a-b-c*d; break; case 4:System.out.println(a+"-"+b+"-"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a-b-f/g; } break; } } break; case 3:{ switch(k){ case 1:System.out.println(a+"-"+b+"*"+c+"+"+d+"=?"); result=a-b*c+d; break; case 2:System.out.println(a+"-"+b+"*"+c+"-"+d+"=?"); result=a-b*c-d; break; case 3:System.out.println(a+"-"+b+"*"+c+"*"+d+"=?"); result=a-b*c*d; break; case 4:System.out.println(a+"-"+b+"*"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a-b*f/g; } break; } } break; case 4:{ switch(k){ case 1:System.out.println(a+"-"+b+"/"+c+"+"+d+"=?"); float f1 = b; float g1 = c; if(g1!=0){ result =a-f1/g1+d; } break; case 2:System.out.println(a+"-"+b+"/"+c+"-"+d+"=?"); float f2 = b; float g2 = c; if(g2!=0){ result =a-f2/g2-d; } break; case 3:System.out.println(a+"-"+b+"/"+c+"*"+d+"=?"); float f3 = b; float g3 = c; if(g3!=0){ result =a-f3/g3*d; } break; case 4:System.out.println(a+"-"+b+"/"+c+"/"+d+"=?"); float f4 = b; float g4 = c; float p4 = d; if(g4!=0 && p4!=0){ result =a-f4/g4/p4; } break; } } break; } } break; case 3:{ switch(j){ case 1:{ switch(k){ case 1:System.out.println(a+"*"+b+"+"+c+"+"+d+"=?"); result=a*b+c+d; break; case 2:System.out.println(a+"*"+b+"+"+c+"-"+d+"=?"); result=a*b+c-d; break; case 3:System.out.println(a+"*"+b+"+"+c+"*"+d+"=?"); result=a*b+c*d; break; case 4:System.out.println(a+"*"+b+"+"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a*b+f/g; } break; } } break; case 2:{ switch(k){ case 1:System.out.println(a+"*"+b+"-"+c+"+"+d+"=?"); result=a*b-c+d; break; case 2:System.out.println(a+"*"+b+"-"+c+"-"+d+"=?"); result=a*b-c-d; break; case 3:System.out.println(a+"*"+b+"-"+c+"*"+d+"=?"); result=a*b-c*d; break; case 4:System.out.println(a+"*"+b+"-"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a*b-f/g; } break; } } break; case 3:{ switch(k){ case 1:System.out.println(a+"*"+b+"*"+c+"+"+d+"=?"); result=a*b*c+d; break; case 2:System.out.println(a+"*"+b+"*"+c+"-"+d+"=?"); result=a*b*c-d; break; case 3:System.out.println(a+"*"+b+"*"+c+"*"+d+"=?"); result=a*b*c*d; break; case 4:System.out.println(a+"*"+b+"*"+c+"/"+d+"=?"); float f = c; float g = d; if(g!=0){ result =a*b*f/g; } break; } } break; case 4:{ switch(k){ case 1:System.out.println(a+"*"+b+"/"+c+"+"+d+"=?"); float f1 = b; float g1 = c; if(g1!=0){ result =a*f1/g1+d; } break; case 2:System.out.println(a+"*"+b+"/"+c+"-"+d+"=?"); float f2 = b; float g2 = c; if(g2!=0){ result =a*f2/g2-d; } break; case 3:System.out.println(a+"*"+b+"/"+c+"*"+d+"=?"); float f3 = b; float g3 = c; if(g3!=0){ result =a*f3/g3*d; } break; case 4:System.out.println(a+"*"+b+"/"+c+"/"+d+"=?"); float f4 = b; float g4 = c; float p4 = d; if(g4!=0 && p4!=0){ result =a*f4/g4/p4; } break; } } break; } } break; case 4:{ switch(j){ case 1:{ switch(k){ case 1:System.out.println(a+"/"+b+"+"+c+"+"+d+"=?"); float f1 = a; float g1 = b; if(g1!=0){ result =f1/g1+c+d; } break; case 2:System.out.println(a+"/"+b+"+"+c+"-"+d+"=?"); float f2 = a; float g2 = b; if(g2!=0){ result =f2/g2+c-d; } break; case 3:System.out.println(a+"/"+b+"+"+c+"*"+d+"=?"); float f3 = a; float g3 = b; if(g3!=0){ result =f3/g3+c*d; } break; case 4:System.out.println(a+"/"+b+"+"+c+"/"+d+"=?"); float f4 = a; float g4 = b; float p4 = c; float q4 = d; if(g4!=0 && q4!=0){ result =f4/g4+p4/q4; } break; } } break; case 2:{ switch(k){ case 1:System.out.println(a+"/"+b+"-"+c+"+"+d+"=?"); float f1 = a; float g1 = b; if(g1!=0){ result =f1/g1-c+d; } break; case 2:System.out.println(a+"/"+b+"-"+c+"-"+d+"=?"); float f2 = a; float g2 = b; if(g2!=0){ result =f2/g2-c-d; } break; case 3:System.out.println(a+"/"+b+"-"+c+"*"+d+"=?"); float f3 = a; float g3 = b; if(g3!=0){ result =f3/g3-c*d; } break; case 4:System.out.println(a+"/"+b+"-"+c+"/"+d+"=?"); float f4 = a; float g4 = b; float p4 = c; float q4 = d; if(g4!=0 && q4!=0){ result =f4/g4-p4/q4; } break; } } break; case 3:{ switch(k){ case 1:System.out.println(a+"/"+b+"*"+c+"+"+d+"=?"); float f1 = a; float g1 = b; if(g1!=0){ result =f1/g1*c+d; } break; case 2:System.out.println(a+"/"+b+"*"+c+"-"+d+"=?"); float f2 = a; float g2 = b; if(g2!=0){ result =f2/g2*c-d; } break; case 3:System.out.println(a+"/"+b+"*"+c+"*"+d+"=?"); float f3 = a; float g3 = b; if(g3!=0){ result =f3/g3*c*d; } break; case 4:System.out.println(a+"/"+b+"*"+c+"/"+d+"=?"); float f4 = a; float g4 = b; float p4 = c; float q4 = d; if(g4!=0 && q4!=0){ result =f4/g4*p4/q4; } break; } } break; case 4:{ switch(k){ case 1:System.out.println(a+"/"+b+"/"+c+"+"+d+"=?"); float f1 = a; float g1 = b; float p1 = c; if(g1!=0 && p1!=0){ result =f1/g1/p1+d; } break; case 2:System.out.println(a+"/"+b+"/"+c+"-"+d+"=?"); float f2 = a; float g2 = b; float p2 = c; if(g2!=0 && p2!=0){ result =f2/g2/p2-d; } break; case 3:System.out.println(a+"/"+b+"/"+c+"*"+d+"=?"); float f3 = a; float g3 = b; float p3 = c; if(g3!=0 && p3!=0){ result =f3/g3/p3*d; } break; case 4:System.out.println(a+"/"+b+"+"+c+"/"+d+"=?"); float f4 = a; float g4 = b; float p4 = c; float q4 = d; if(g4!=0 && p4!=0 && q4!=0){ result =f4/g4/p4/q4; } break; } } break; } } break; } return result; }

    2、建立calculate()方法,确定生成题目的数量,并对结果进行判断,统计答对的题目数

       public void calculate(){
            int h = sc.nextInt();                  //题目数量
            for(int m=0;m<h;m++){               //for循环用来生成指定数量的题目
                double result = addQuestion();
                float jg = sc.nextFloat();                  //将自算结果赋给jg
                if(jg==result){                             //用if,else对结果进行判断,正确则继续,若错误则显示正确答案,然后继续
                    System.out.println("答对了,你真是个天才!");
                    right++;
                }else{
                    System.out.println("结果错误!");
                    System.out.println("再想想吧,答案似乎是:"+result+"喔!");
                    }
            }
            System.out.println("你一共答对"+right+"道题,"+"共"+h+"道题。");
        }

    3、建立choose()方法,选择是否继续答题或结束答题

       public void choose(){
            System.out.println("1.开始下一轮	2.结束答题");
            Scanner select = new Scanner(System.in);
            int se = select.nextInt();
            switch(se){                                 //两层switch,case判断,
            case 1:main(null);                          //如果两次选择均输入错误,则结束程序
                    break;
            case 2:System.out.println("答题结束!");
                    break;
            default:System.out.println("输入有误,请重新输入!");
                    System.out.println("1.开始下一轮	2.结束答题");
                    Scanner rp = new Scanner(System.in);
                    int rt = rp.nextInt();
                    switch(rt){
                    case 1:main(null);
                            break;
                    case 2:System.out.println("答题结束!");
                            break;
                    default:System.out.println("连续两次输入错误,答题结束!");
                    }
            }
        }

    4、main(),调用其它方法

       public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("*******随机四则运算*******");
            System.out.println("请输入题目数量:");
            one js = new one();
            js.calculate();
            js.choose();
            
        }

    测试用例

      对addQuestion()方法进行测试。上面也说了,为了方便测试,将随机数和随机符号进行了固定。这样结果我们是知道的,如果我们的希望就是这个结果,那么就会成功通过测试,如果换个数,就不会通过测试。

    成功情况:其它都是已知的,所以期望为-9。

      @Test
        public void testAddQuestion() {
            szys3 ys = new szys3();
            double expected = -9;
            assertEquals(expected,ys.addQuestion(),1);
        }

    测试通过:

    失败情况:期望改为-7,不是正确答案

      @Test
        public void testAddQuestion() {
            szys3 ys = new szys3();
            double expected = -7;
            assertEquals(expected,ys.addQuestion(),1);
        }

    测试结果:

    HTTP:https://git.coding.net/yanzouzhe/szys.git

    SSH:git@git.coding.net:yanzouzhe/szys.git

    照片:

    体会:要求就理解到这种程度,对于我们这种水平很低的人来说,做测试还是非常费劲的。

  • 相关阅读:
    狗狗急性肠胃炎
    shell change password
    男职工为什么要交生育保险
    预扣预缴个税
    转《最优状态机》
    状态机编程-队列缓冲事件,事件可异步触发
    max713镍氢电池充电管理IC
    NUP2201MR
    LSP5513
    74HC14D(6反向施密特触发器)
  • 原文地址:https://www.cnblogs.com/huangxman/p/5918346.html
Copyright © 2011-2022 走看看