码迷,mamicode.com
首页 > 其他好文 > 详细

(第四周)四则运算单元测试

时间:2016-09-29 02:02:38      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

项目成员:黄兴、谢孝淼

程序代码

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

1、生成题目并计算的addQuestion()方法,其中把随机数和随机运算符都设成了固定值,这样是为了更容易的进行单元测试,除了这个方式,还没有想好怎样对随机数的方法进行测试。

   Scanner sc = new Scanner(System.in);   //创建Scanner类的对象
    double result=0;
    int right=0;                           //统计正确题目的数量
    public double addQuestion(){
//      Random r = new Random();       //生成一个100以内随机数的方法
//      int a = r.nextInt(100);
//      int b = r.nextInt(100);
//      int c = r.nextInt(100);
//      int d = r.nextInt(100);
        int a = 1;
        int b = 2;
        int c = 3;
        int d = 4;
//      Random r2 = new Random();
//      int i = r2.nextInt(3) + 1;             //另一个生成随机数1~4的方法
//      int j = r2.nextInt(3) + 1;               //另一个生成随机数1~4的方法
//      int k = r2.nextInt(3) + 1;               //另一个生成随机数1~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.开始下一轮\t2.结束答题");
        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.开始下一轮\t2.结束答题");
                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

照片:保密

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

 

(第四周)四则运算单元测试

标签:

原文地址:http://www.cnblogs.com/huangxman/p/5918346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!