我需要编写一个名为 Continuous 的方法,该方法接受 6 个参数来检查 6 个输入是否是连续数字,而不管顺序如何。如果数字是连续的,则返回 true,否则返回 false。有人可以教我什么是最有效的方法吗?我尝试了这种方法,但如果我有 6 个数字,它会效率低下吗?
public static boolean consecutive(int numberOne,int numberTwo,int numberThree) {
if(((numberTwo==numberOne-1) && (numberThree== numberTwo-1)) || ((numberTwo==numberOne+1) && (numberThree==numberTwo+1)) || ((numberTwo==numberOne+2) &&( numberThree==numberOne+1)) || ((numberTwo==numberOne-1)&& (numberThree==numberOne+1)) || ((numberTwo==numberOne-2) && (numberThree==numberOne-1)) ||((numberTwo==numberOne+1) && (numberThree==numberOne-1))){
return true;
}else{
return false;
}
}
}