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

单元测试(补发)

时间:2015-06-22 23:28:23      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

设计思想:

通过单元测试对每一个单元测试看结果与预期结果是否相同.不重复的实现,建三个数组,并每次生成随机数后判断a[i]b[i]c[i]与上边已有的a[i]b[i]c[i]是否相同,若相同重新生成a[i]b[i]c[

for(j=0;j<z;j++)    
            {
                if(q==a[j]&&w==b[j]&&e==c[j])       ////判断是否相同
                    {a[i]=random.nextInt(100);      ////重新产生a[i]
                    b[i]=random.nextInt(100);       ////重新产生b[i]
                    c[i]=random.nextInt()%4+1;} ////重新产生c[i]
            }
可定制的实现,输入一个n,比如n=4,当(i+1)%4==0时,输出一个换行,这样就实现了每行有4个算式。
是否有乘除法的实现,若有乘除法,c取1234,若无乘除c取12.
c取1234时:
if      (c[i]==1) s=‘+‘;
        else if (c[i]==2) s=‘-‘;
        else if (c[i]==3) s=‘*‘;
        else s=‘/‘;
c取12时:
if      (c[i]==1) s=‘+‘;
        else s=‘-‘;
 
实现代码:
package 四则运算;
 
import java.util.Random;
import java.util.Scanner;
 
public class aa {
 
         public static void main(String[] args) {
                   // TODO Auto-generated method stub
                   int i,n,j,m,aa,r = 0;
                   Scanner scanner = new Scanner(System.in);
                   System.out.print("要输出多少道四则运算题,然后敲回车");
                   aa= scanner.nextInt();
                   i=aa;
                   System.out.print("请输入n(每行显示n个式子),然后敲回车");
                   n= scanner.nextInt();
                   System.out.print("是否有乘除法(有 1    无 0),然后敲回车");
                   m= scanner.nextInt();
                   if(m==1)
                            {
                            System.out.print("除法有无余数(有 1    无 0),然后敲回车");
                            r= scanner.nextInt();
                            }
                   scanner.close();
                   int a[],b[],c[];
                   a=new int[i];b=new int[i];c=new int[i];
                   for(i=0;i<aa;i++)
                   {
                   int z,q,w,e;z=i;
                   char s;
                   Random random = new Random();
                   a[i]=random.nextInt(100);
                   b[i]=random.nextInt(100);
 
                   if(m==1)
                   {
                            if(r==1)//有余数
                            {
                                     c[i]=random.nextInt()%4+1;
                                     q=a[i];w=b[i];e=c[i];
                                     for(j=0;j<z;j++)
                                     {
                                               if(q==a[j]&&w==b[j]&&e==c[j])
                                                        {a[i]=random.nextInt(100);
                                                        b[i]=random.nextInt(100);
                                                        c[i]=random.nextInt()%4+1;}
                                     }
                                     if               (c[i]==1) s=‘+‘;
                                     else if (c[i]==2) s=‘-‘;
                                     else if (c[i]==3) s=‘*‘;
                                     else s=‘/‘;
                            }
                            else //无余数
                            {
                            a[i]=random.nextInt(100);
                            b[i]=random.nextInt(100);
                            c[i]=random.nextInt()%4+1;
                            q=a[i];w=b[i];e=c[i];
                            for(j=0;j<z;j++)
                                     {
                                               if(q==a[j]&&w==b[j]&&e==c[j])
                                                        {a[i]=random.nextInt(100);
                                                        b[i]=random.nextInt(100);
                                                        c[i]=random.nextInt()%4+1;}
                                     }
                            if               (c[i]==1) s=‘+‘;
                            else if (c[i]==2) s=‘-‘;
                            else if (c[i]==3) s=‘*‘;
                            else {s=‘/‘;a[i]=a[i]/b[i]*b[i];}
                            }
                   }
                   else
                   {
                   c[i]=random.nextInt()%2+1;
                   q=a[i];w=b[i];e=c[i];
                   for(j=0;j<z;j++)
                            {
                                     if(q==a[j]&&w==b[j]&&e==c[j])
                                               {a[i]=random.nextInt(100);
                                               b[i]=random.nextInt(100);
                                               c[i]=random.nextInt()%2+1;}
                            }
                   if               (c[i]==1) s=‘+‘;
                   else s=‘-‘;
                   }
                  
                   System.out.print(a[i]);
                   System.out.print(s);
                   System.out.print(b[i]);
                   System.out.print("=     ");
                   if((i+1)%n==0)
                   System.out.println("");
                   }
         }
 
}

技术分享i]



单元测试(补发)

标签:

原文地址:http://www.cnblogs.com/xizhenghe/p/4594207.html

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