我有一个整数数组,我想检查是否有任何两个连续元素具有不同的符号。我试过这个,但它不起作用
例如,{2,-3,17,-1,4,5,-1,8,-9,3}
必须为 false 但方法给出 true
public boolean alternance(int[] a) {
for(int i = 0; i < a.length - 1; i++) {
if(a[i] * a[i+1] < 0)
return true;
}
return false;
}
最佳答案
我不确定这是否正是您要查找的内容,但下面的方法将为 {2,-3,17,-1,4,5,-1,8,-9,3} 返回 false
public boolean alternance(int[] a) {
boolean isAlternance = true;
for(int i = 0; i < a.length - 1; i++) {
if(a[i] * a[i+1] >= 0)
isAlternance = false;
}
return isAlternance;
}
关于Java交替方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37801039/