Practice numerical calculations. Find the maximum value of the sum of the neutron arrays of an integer array.

Source: Internet
Author: User

Method class:

Package com.lwk.test2;

Import Java.util.Scanner;

public class Sum {

Scanner input = new Scanner (system.in);

public int sumparams (int nums[]) {
int maxsum = 0;
for (int i = 0; i < nums.length; i++) {
int sum = 0;
for (int j = i; J < Nums.length; J + +) {
sum = sum + nums[j];
if (Sum > Maxsum) {
Maxsum = sum;
}
}
}
System.out.println ("The maximum value of the sum of the Subarray is:" +maxsum);
return maxsum;

}
}

Test class:

Package com.lwk.test2;

Import static org.junit.assert.*;

Import Org.junit.Test;

public class Sumtest {

@Test
public void Test () {
/* int nums[] = {1,-2,3,5,-1}; *///test Case 1 Maximum value 8
/* int nums[] = {1,-2,3,-8,5,1}; *///test Case 1 Maximum value 6
int nums[] = {1,-2, 3,-2, 5, 1}; Test Case 1 Maximum value 7
Sum sum = new sum ();
Sum.sumparams (Nums);
}

}

There are three test cases, you can add different test cases according to your needs

Practice numerical calculations. Find the maximum value of the sum of the neutron arrays of an integer array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.