好吧,所以我有下面的代碼,不管它返回給我一個-1。我想擁有它,這樣如果id匹配,那麼它將返回並返回索引,但如果它在遍歷整個數據集後不匹配,它將返回一個負值。我要去哪裏錯在這裏:Java返回的困境
public class StudentCollection {
private String[] ids = new String[] {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty"}; // keeps identification numbers of students
private String [] names = new String[] {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty"};; // keeps the names of students
private int size = 0; // number of students currently in the collection
private int findIndex(String id) {
int noIndex = 1;
for (int i=0;i<ids.length;i++){
if((ids[i].equalsIgnoreCase(id))){
System.out.println("The index of this student is " +i);
}
else {
noIndex = -1;
System.out.println(noIndex);
break;}
}
return noIndex;
}
代碼中有什麼'ids'? –
提示:您在哪裏將noIndex設置爲要返回的值?你什麼時候該休息?爲什麼你否定equalsIgnoreCase的結果? – samgak