Java-在数组中查找元素

Java-在数组中查找元素,java,arrays,indexing,Java,Arrays,Indexing,我目前正在尝试找出如何搜索我的数组以查找数组中的特定元素。用户将输入他们想要查找的名称,我的程序将返回给用户他们所坐的座位。通过另一种方法添加乘客。因此,如果我有一个叫“Tim Jones”的人坐在5号座位上,如果我使用这种方法,我会输入“Tim Jones”,程序会告诉我Tim Jones坐在5号座位上 我得到的当前输出只是else语句,无论我怎么做都找不到乘客。有什么建议吗?提前谢谢 private static void findPassengerSeat(String airplaneR

我目前正在尝试找出如何搜索我的数组以查找数组中的特定元素。用户将输入他们想要查找的名称,我的程序将返回给用户他们所坐的座位。通过另一种方法添加乘客。因此,如果我有一个叫“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;
        }
    }
}
private static void findPassengerSeat(字符串airplaneRef[]){
字符串passengerName;
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入乘客姓名”);//要求用户输入乘客姓名。
passengerName=input.next();//用户输入存储在变量passengerName下。
对于(int i=0;i
您应该去掉
else
,否则您只能计算数组的第一个索引,并通过所有剩余的索引

下面这句话应该很有意思:你基本上浏览了所有乘客的列表,只有当他们中没有一个与输入相对应时,你才声明没有找到乘客

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.
        return;
    }
}
System.out.println("Passenger not found.");
for(int i=0;i
您应该去掉
else
,否则您只能计算数组的第一个索引,并通过所有剩余的索引

下面这句话应该很有意思:你基本上浏览了所有乘客的列表,只有当他们中没有一个与输入相对应时,你才声明没有找到乘客

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.
        return;
    }
}
System.out.println("Passenger not found.");
for(int i=0;i
您的数组中可能有一个Tim,但由于使用了
next
而不是
nextLine
,因此您没有一个Tim Jones。这就是为什么您应该学习如何使用调试器的原因。您的数组中可能有一个Tim,但由于使用了
next
而不是
nextLine
,你没有Tim Jones。这就是为什么你应该学习如何使用调试器。