我想得到一个数组中偶数的总和:
public static void main(String[] args) {
int[] array = new int[4];
array[0] = 1;
array[1] = 2;
array[2] = 2;
array[3] = 4;
System.out.println("Count even: " + countE(array, 0));
}
public static int countE(int[] arr, int head) {
if (arr.length == head) {
return -1;
} else if (arr[head] % 2 == 0) {
return 1 + countE(arr, head + 1);
} else {
return 0 + countE(arr, head + 1);
}
}