我写了这个递归方法来查找整数数组中的整数,但它不起作用。我试着调试它,但我不知道问题会是什么。找到整数数组中的整数元素:递归问题
下面的代码
public static String inList(int[] primes,int a){
int index = -9;
if(primes.length>1){
index = primes.length/2;
}else{
if(primes[0] == a){
return "True";
}else{
return "False";
}
}
if(primes[index] == a){
return "True";
}
if(primes[index] > a){
inList(Arrays.copyOfRange(primes, 0, index),a);
}
if(primes[index]<a){
inList(Arrays.copyOfRange(primes, index, primes.length),a);
}
//shouldn't even get to this point, but eclipse insisted I needed another return
//statement
return "Whyyyyy?";
}
“不起作用”是什么意思?错误的结果?一个错误?另外,我认为你的意思是返回递归调用的值。 (这就是Eclipse抱怨的原因。) – Ryan
为什么一个名为'inList'的函数返回一个'String'?它不应该在列表中,还是不在? – Ryan
为什么你要返回'“True”而不是'true'? –