求偶数元素位置的数据累计和

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;
}

}

上一篇:2021.5 SD省选一轮培训游记


下一篇:You Can’t Sacrifice Partition Tolerance