求偶数元素位置的数据累计和
作者:互联网
package com.itheima.demo01;
public class Bug07 {
public static void main(String[] args) {
int[] arr = {20, 22, 23, 24, 28, 26};
int sum = getSum(arr);
System.out.println(sum);
}
public static int getSum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i+=2) {
if (arr[i] % 2 == 0 ) {
sum += arr[i];
}
}
return sum;
}
}
标签:arr,int,sum,元素,累计,求偶,getSum,static,public 来源: https://www.cnblogs.com/lvtiantian/p/14724699.html