Unit testing is very important when writing a program, and the following is the code implementation and Unit test section for the maximum value of the array and the result screenshot
1 ImportJava.util.Scanner;2 Public classTest {3 Public Static voidMain (string[] args) {4 inta[]={1,2,3,4,5,6};5Judge (Largest (a,6));6 inta2[]={9,4,0,2,5,10};7Judge (Largest (a2,6));8 int[] a3={000,-1,-88,3,000,999};9Judge (Largest (a3,6));Ten inta4[]={}; OneJudge (Largest (a4,0)); A int[] a5=New int[5]; -Judge (Largest (a5,5)); - while(1>0){ theSystem.out.println ("\ n Please enter the length of the array"); -Scanner sc2=NewScanner (system.in); - intLength=sc2.nextint (); - inta[]=New int[length]; +System.out.println ("Please enter an array"); -Scanner sc=NewScanner (system.in); + for(inti=0;i<length;i++) A { ata[i]=sc.nextint (); - } - judge (Largest (a,length)); - } - } - Public Static intLargest (intList[],intlength) { in if(list==NULL|| Length==0){ - return0; to } + intI,max=list[0]; - for(i=0;i<length;i++){ the if(list[i]>max) { *max=List[i]; $ }Panax Notoginseng } -System.out.print ("\ n" +max); the return1; + } A Public Static voidJudgeinti) { the if(i==0){ +System.out.print ("\ n array is empty! "); - } $ Else{ $System.out.print ("is the maximum value"); - } - } the}
Know in
These test arrays run with the following results:
Unit Test Example