Java 在循环中比较ArrayList的元素会得到意外的结果吗?

Java 在循环中比较ArrayList的元素会得到意外的结果吗?,java,arraylist,foreach,java.util.scanner,Java,Arraylist,Foreach,Java.util.scanner,以下代码仅检查ArrayList中的第一项。当我键入ArrayList中但不在第一个位置的项目时,我会收到错误消息“请输入有效名称” 我怎样才能解决这个问题?谢谢大家! 这是我的密码: private ArrayList<Account> accounts = new ArrayList<>(); for(Account a : accounts) { while(true) { System.

以下代码仅检查ArrayList中的第一项。当我键入ArrayList中但不在第一个位置的项目时,我会收到错误消息“请输入有效名称”

我怎样才能解决这个问题?谢谢大家!

这是我的密码:

   private ArrayList<Account> accounts = new ArrayList<>();

   for(Account a : accounts)
    {
        while(true)
        {
            System.out.printf("Customer name: ");
            String customerName = scanner.next();

            if(customerName.equals(a.getName()))
            {
                System.out.println("You entered " + a.getName());
                break;
            }
            else
            {
                System.out.println("Please enter a valid name");
            }
        }
    }   
private ArrayList accounts=new ArrayList();
(账户a:账户)
{
while(true)
{
System.out.printf(“客户名称:”);
字符串customerName=scanner.next();
if(customerName.equals(a.getName()))
{
System.out.println(“您输入了”+a.getName());
打破
}
其他的
{
System.out.println(“请输入有效名称”);
}
}
}   

内部循环执行以下操作:

while(true) {
在这里没有意义。它只是在外循环中保持循环,因此总是与相同的
a
帐户进行比较


基本上你必须交换两个循环

问题在于,您一直只检查第一个元素。在您输入第一个元素(并在(1)时中断循环)之后,您将进入第二个元素

想象一下你的arrayList中有

"hello", "bye"
在发送第一个元素(“hello”)之前,您将一直处于循环中

解决方案:

 while(true)
         {
             System.out.printf("Customer name: ");
             String customerName = scanner.next();
             if (accounts.contains(customerName)){
                 System.out.println("You entered " + customerName);
                 break;
             }
             else{
                 System.out.println("Please enter a valid name");
             }
         }

问题是无限while循环

while(true)
只有当
customerName==firstElement.Name
时,此循环才会中断,否则它是一个无限循环。相反,我认为您想要尝试的是将while循环移到for循环之外。所以代码看起来像这样

    private ArrayList<Account> accounts = new ArrayList<>();

    while(true)
    {
        System.out.printf("Customer name: ");
        String customerName = scanner.next();
        for(Account a : accounts){

           if(customerName.equals(a.getName())){
                 System.out.println("You entered " + a.getName());
                 break;
           }else{
            System.out.println("Please enter a valid name");
           }
        }
    }
private ArrayList accounts=new ArrayList();
while(true)
{
System.out.printf(“客户名称:”);
字符串customerName=scanner.next();
(账户a:账户){
if(customerName.equals(a.getName())){
System.out.println(“您输入了”+a.getName());
打破
}否则{
System.out.println(“请输入有效名称”);
}
}
}

你必须在休息时中断。当您在列表上迭代时,您必须考虑逻辑。它可以是这样的代码

ArrayList<Account> accounts = new ArrayList<>();
boolean isMatched = false;

while (true) {
    for (Account account : accounts) {
        System.out.printf("Customer name: ");
        String customerName = scanner.next();
        if (customerName.equals(account.getName())) {
            isMatched = true;
            break;
        }
    }
    if (isMatched) {
        System.out.println("You entered " + account.getName());
        break;
    }
    System.out.println("Please enter a valid name");
}
ArrayList accounts=new ArrayList();
布尔值isMatched=false;
while(true){
用于(账户:账户){
System.out.printf(“客户名称:”);
字符串customerName=scanner.next();
if(customerName.equals(account.getName())){
isMatched=true;
打破
}
}
如果(已匹配){
System.out.println(“您输入了”+account.getName());
打破
}
System.out.println(“请输入有效名称”);
}

PS:
boolean
在找到结束while循环的客户名称时的值。

提示:使用合适的命名:
a
真的没有意义。称它为
account
,或
currentAccount
或其他什么。我刚刚注意到你获得了投票权。这使您不仅可以接受答案,还可以对有用的答案表示感谢,因为您现在还可以对它们进行投票;-)我已经删除了while循环,但同样的问题仍在发生:(例如,如果我有两个名字“Tom”和“Sam”,第一次输入Sam时,它会说无效的名字。第二次输入Sam时,它会接受。是的,我知道这是怎么回事,但我如何解决它?删除:customerName.equals(a.getName())并添加accounts.contains(customerName)您必须删除(帐户a:帐户)的循环还有。谢谢你的回答。它工作得稍微好一点,但现在的问题是,如果我在arrayList中输入第三个名称,它将重复输入其他两个名称的消息,直到它达到正确的名称。这是为什么?我更新了帖子。你能检查@sg9吗?我将打印逻辑更改为for循环的结尾。
while(true)