Created
December 18, 2015 12:00
-
-
Save Yur-ok/70a69381db2fe1f3ee0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Lesson3.KeyPoint2; | |
/** | |
* Created by Юрий on 18.12.2015. | |
*/ | |
public class OddElementSum { | |
public static void main(String[] args) { | |
int[] arr = {1, 2, 3, 4, 5}; | |
int[] arr1 = {1, 2}; | |
int[] arr2 = {1}; | |
int[] arr4 = null; | |
System.out.println(oddElementSum(arr)); | |
System.out.println(oddElementSum(arr1)); | |
System.out.println(oddElementSum(arr2)); | |
System.out.println(oddElementSum(arr4)); | |
} | |
static long oddElementSum(int data[]) { | |
if (data != null && data.length >= 2) { | |
int sum = 0; | |
for (int i = 1; i < data.length; i += 2) { | |
sum += data[i]; | |
} | |
return sum; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment