单元测试示例

在写程序的时候单元测试十分重要,下面是求数组最大值的代码实现和单元测试部分以及结果截屏

 1 import java.util.Scanner;
 2 public class Test {
 3     public static void main(String[] args){
 4         int a[]={1,2,3,4,5,6};
 5         judge(Largest(a,6));
 6         int a2[]={9,4,0,2,5,10};
 7         judge(Largest(a2,6));
 8         int[] a3={000,-1,-88,3,000,999};
 9         judge(Largest(a3,6));
10         int a4[]={};
11         judge(Largest(a4,0));
12         int[] a5=new int[5];
13         judge(Largest(a5,5));
14         while(1>0){
15             System.out.println( "\n请输入数组的长度");
16             Scanner sc2=new Scanner(System.in);
17             int length=sc2.nextInt();
18             int A[]=new int[length];
19             System.out.println( "请输入一个数组");
20             Scanner sc=new Scanner(System.in);
21             for(int i=0;i<length;i++)
22             {
23                  A[i]=sc.nextInt();
24             }
25             judge(Largest(A,length));            
26         }
27     }
28     public static int Largest(int list[],int length){    
29         if(list==null||length==0){
30                 return 0;
31         }
32         int i,max=list[0];
33         for(i=0;i<length;i++){
34             if(list[i]>max){
35                 max=list[i];
36             }
37         }
38         System.out.print( "\n"+max);
39         return 1;
40     }
41     public static void judge(int i){
42         if(i==0){
43             System.out.print( "\n数组为空!");
44         }
45         else{
46             System.out.print( "是最大值");
47         }
48     }
49 }

可知在

这几个测试数组运行结果为:

转载于:https://www.cnblogs.com/amiee/p/5302725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值