1  1 package com.example.tony.testforgit;
 2  2 
 3  3 /**
 4  4  * Created by Tony on 2017/3/17.
 5  5  */
 6  6 public class Array {
 7  7     public  int maxArr(int[] arr){
 8  8         int newSum = arr[0];
 9  9         int maxSum = arr[0];
10 10         for(int i=1;i<arr.length;i++){
11 11             newSum += arr[i];
12 12             if(newSum<arr[i]){
13 13                 newSum = arr[i];
14 14             }
15 15             if(maxSum<newSum){
16 16                 maxSum = newSum;
17 17             }
18 18         }
19 19         return maxSum;
20 20     }
21 21 }

生成测试类,编辑测试用例

 1 package com.example.tony.testforgit;
 2 
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 
 6 import static org.junit.Assert.*;
 7 
 8 /**
 9  * Created by Tony on 2017/3/17.
10  */
11 public class ArrayTest {
12     private Array array;
13 
14     int arr1[] ={1,2,3,-1,2};
15     
16     @Before
17     public void setUp() throws Exception {
18        array = new Array();
19     }
20 
21     @Test
22     public void testMaxArr() throws Exception {
23         assertEquals(7d,array.maxArr(arr1),0);
24 
25 
26     }
27 }

测试结果:成功

在Android studio中,测试输出数组中最大子数组的和

相关文章:

  • 2022-01-02
  • 2021-09-07
  • 2021-05-31
  • 2021-12-30
  • 2021-05-19
  • 2022-01-16
  • 2021-09-15
猜你喜欢
  • 2022-02-17
  • 2022-02-27
  • 2021-11-25
  • 2021-11-22
  • 2022-12-23
相关资源
相似解决方案