单元测试(输入一个数组和它的大小,输出这个数组中最大子数组的和)

1.Calculate 

public class Calculate {
public static void main(String[] args){
Calculate i =new Calculate();
int Max = 0;
int Max1 = 0;
Scanner in=new Scanner(System.in);
System.out.print("请输入数组的长度:");
int j=in.nextInt();
System.out.print("请依次输入数组:");
int Arr[]=new int[j];
for(int a = 0;a < j; a++){
Arr[a]=in.nextInt();
}
int Arr1[]=new int [j];
for(int a = 0; a < j; a++) {
for(int b = 0; b < j; b++){
Arr1[b]=Arr[(b + a)%(j)];
}
Max=i.findMaxArr(Arr1);
if(Max > Max1) {
Max1 = Max;
}
}
System.out.print("最大的子数组和为"+Max1);
}
public int findMaxArr(int[] arr) {
int Arr = 0;
int MaxArr = 0;
int c = arr.length;
int Location1=0;
int Location2=0;
int a;

for ( a = 0; a < c; a++) {
Arr += arr[a];
if (Arr < 0) {
Arr = 0;
}
if (Arr > MaxArr) {
MaxArr = Arr;
}
}

for(a = Location2; a >= 0; a--) {
if(MaxArr-arr[a]==0) {
Location1 = a;
break;
}
}
if (MaxArr == 0) {
for ( a = 0; a < c; a++) {
if (a == 0) {
MaxArr = arr[a];
}
if (arr[a] > MaxArr) {
MaxArr = arr[a];
}
}
}
return MaxArr;
}
}

 



2.CalcuateTest
public class CalculateTest {
private Calculate mcalculate;
int[] Calculate = {-1,2,-5,3,-4};
@Before
public void setUp() throws Exception {
mcalculate =new Calculate();
}

@Test
public void testMain() throws Exception {

}

@Test
public void testFindMaxArr() throws Exception {
assertEquals(3, mcalculate.findMaxArr(Calculate), 0);

}
3.测试结果截图

 

 
posted @ 2017-03-16 14:13  李苏南  阅读(343)  评论(0编辑  收藏  举报