【发布时间】:2016-05-01 20:33:14
【问题描述】:
当我遇到这个问题时,我正在使用 CodingBat 练习,我无法完全正确。给定一个正整数数组,我必须创建一个长度为“count”的数组,其中包含原始数组中的第一个偶数。原始数组将至少包含“count”个偶数。我的代码在下面,虽然我知道第一个 if 语句下面的所有内容都不起作用,并且“counter”变量基本上没用。
public int[] copyEvens(int[] nums, int count) {
int counter = 0;
int[] countArr = new int [count];
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0) {
//what to put here?
}
}
return countArr;
}
任何帮助将不胜感激。
【问题讨论】:
-
如果为 0 表示可用
-
我没有在代码中看到问题