课堂作业数组(一)

 

 

public class test {
     public static void main(String args[]) {  

           int array[] = {0,-2,3,10,-4,7,2};  
           System.out.println(findMax(array));

        }  

        public static int findMax(int array[]){

            if (array.length == 0) {
                return 0;
            }

            int max = array[0];
            int sum = 0;

            for(int i=0; i<array.length; i++){  
               
                if(sum >= 0) { 
                    sum += array[i];  
                }
                else{  
                    sum = array[i];
                }
                if(sum > max){  
                    max = sum;  
                }
            }  
            return max;  
        }

}

 

posted @ 2022-03-11 23:28  zrswheart  阅读(25)  评论(0编辑  收藏  举报