0
我有任务建立一个方法来搜索数组中的变量的相同值。 当有匹配时,该方法将返回索引 - 位置,否则应返回-1。使用while循环搜索数组中的特定值
我的方法在匹配时有效,但当没有任何匹配时我收到错误。
到目前为止我的代码:
public class Schleifentest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] cherry = {7,5,6,8,9};
int magNumber = 112;
int enthalten2 = Schleifentest.sucheWhile(cherry, magNumber);
System.out.println(enthalten2);
}
public static int sucheWhile(int [] array, int a) {
int i = 0;
while(i <= array.length) {
if (array[i] == a) {
return i;
}
i++;
}
// here is the problem
return -1;
}
}
感谢您的帮助。 菲尔
什么是错误? – Bathsheba