求偶数元素位置的数据累加和
作者:互联网
package com.itheima.demo01;
public class Bug06 {
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++) {
if (i % 2 == 0 && arr[i] % 2 == 0) {
sum += arr[i];
}
}
return sum;
}
}
标签:arr,int,sum,元素,累加,求偶,getSum,static,public 来源: https://www.cnblogs.com/lvtiantian/p/14724702.html