第二次作业

Junit测试

用java求子列最大值

(1)安装好的Eclipse

(2)测试代码及截图

代码:

package liuchen.practice;
import java.util.Scanner;

public class GetGreatestSub {
	public int Find(int[] array) {
        if (array == null || array.length == 0) {
            return Integer.MIN_VALUE;
        }
        int currSum = Integer.MIN_VALUE;
        int maxSum = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            currSum = (currSum < 0) ? array[i] : currSum + array[i];
            if (currSum > maxSum) maxSum = currSum;
        }
        return maxSum;
    }
	
	public static void main(String[] args) {		
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine().toString();
		String arrString[] = str.split(",");
		int[] arr= new int[arrString.length];
		for(int i = 0; i < arrString.length; i++){
			arr[i] = Integer.parseInt(arrString[i]);
		}
		scanner.close();
		
		GetGreatestSub g = new GetGreatestSub();
		int result = g.Find(arr);
		System.out.println("最大子列的值为:"+result);
	}
}

截图:

(3) 测试类代码及截图

代码:

package liuchen.practice;

import static org.junit.Assert.*;

public class Test {

	@org.junit.Test
	public void test() {
		int arr[] = new int[]{5,-8,48,-89,9,4,-123,69};
		int result = new GetGreatestSub().Find(arr);
		assertEquals(69,result);
	}

}

截图:

(4) 测试结果截图

posted @ 2019-04-13 19:01  YNlcGZxhy  阅读(80)  评论(0编辑  收藏  举报