我目前正在尝试弄清楚如何搜索我的数组以查找数组中的特定元素。用户将输入他们要查找的名称,我的程序应该将他们所坐的座位返回给用户。添加乘客是通过另一种方法完成的。所以,如果我有一个叫“Tim Jones”的人坐在 5 号座位上,如果我使用这种方法,我会输入“Tim Jones”,程序应该告诉我 Tim Jones 坐在 5 号座位上。
我得到的当前输出只是 else 语句,无论我尝试什么都找不到乘客。有小费吗?提前致谢。
private static void findPassengerSeat(String airplaneRef[]) {
String passengerName;
Scanner input = new Scanner(System.in);
System.out.println("Enter passenger's name."); // asks user for passenger name.
passengerName = input.next(); // user input stored under variable passengerName.
for (int i = 0; i < airplaneRef.length; i++) { // for i, if i is less than the length of the array airplaneRef, increment i.
if (airplaneRef[i].equalsIgnoreCase(passengerName)) { // iterates through all elements in the array, ignoring case sensitivity and looks for the passenger.
int seat = i;
System.out.println(passengerName + " is sitting in seat s" + seat); // prints name of passenger and what seat they are sitting in.
} else {
System.out.println("Passenger not found.");
break;
}
}
}
慕桂英3389331
相关分类