java只返回奇数数组
我只需要返回一个奇数数组,例如[1,3,5]
。作为学校教育的一部分,我被要求这样做,但我看不出哪里出了问题
public static int[] odds(int numOdds) {
int[] odds = numOdds;
for (int i=0; i<odds.length; i++) {
if (odds[i] %2 != 0) {
return odds;
}
}
}
public static void main(String[] args) {
int[] theOdds = odds(3);
System.out.println(theOdds[0] + ", " + theOdds[1] + ", " + theOdds[2]);
}
# 1 楼答案
这是你发布的代码。见下面我的评论