所以我有一个家庭作业问题。这就是我需要做的事情:
写一个方法的定义,oddsMatchEvens,其两个参数是相等大小的整数数组。每个数组的大小是偶数。当且仅当第一个数组的偶数索引元素按顺序等于第二个数组的奇数索引元素时,该方法返回true。也就是说,如果w是第一个数组,q是第二个数组,w [0]等于q [1],w [2]等于q [3],依此类推。
我拥有的是:
public boolean oddsMatchEvens(int[] w, int[] q){
int count = 0;
for(int i=0; i < w.length; i++){
if(w[i].equals(q[i + 1])){
count++;
if(count == w.length){
return true;
}
}
}
我收到此错误:
⇒ You almost certainly should be using: &&
⇒ You almost certainly should be using: +=
⇒ You almost certainly should be using: >=
答案 0 :(得分:1)
试试这个:
if(w[i] == q[i + 1])
&#39;等于#39;方法用于对象。
当然,必须始终返回布尔值。
该方法的工作版本:
public boolean oddsMatchEvens(int[] w, int[] q) {
int count = 0;
for (int i = 0; i < w.length; i++) {
if (w[i] == q[i + 1]) {
count++;
if (count == w.length) {
return true;
}
}
}
return false;
}
答案 1 :(得分:1)
public boolean oddsMatchEvens(int[] w, int[] q){
int count = 0;
for(int i=0; w.length >= i; i++){
count += 1;
if(w[i] == q[i + 1] && count == w.length){
return true;
}
}
}
答案 2 :(得分:1)
你应该把回报放在for循环之外。
for(int i=0; i < w.length; i++){.....}
return count==w.length;