Java for 循环 with if 语句只迭代一次

Java for loop with if statement only iterating once

我有一个我想制作的员工系统登录版本,我有一个 for 循环,它应该遍历整个帐户列表,然后查看员工的姓名是否与列表中的姓名匹配然后 if 语句继续,提出更多问题等等......它似乎只迭代一次然后停止,因为它只会找到第一个用户并告诉我其他帐户不存在,即使它们存在!我究竟做错了什么?我的列表还包含从 Account 继承的 Employees 和 Managers,if 语句使用 Account 中的 getName 来比较它是否等于用户输入。对不起,如果这太荒谬了 stupid/bad!谢谢。

List <Account> Accounts = new LinkedList<Account>();   

这里是我填充我的帐户的地方,主要方法调用它并调用 list() whihc 包含有问题的循环

public void add() {
    Employee Geoff = new Employee("Geoff", "password1");
    Manager Bob = new Manager("Bob", "password2");
    Employee John = new Employee("John", "password3");

    Accounts.add(Geoff);
    Accounts.add(Bob);
    Accounts.add(John);
    list();

    }

问题:

System.out.println("Hello welcome: ");
System.out.println("Please enter your name: ");
String empName = Scan.nextLine();

for (Account a : Accounts) { 
        System.out.println(a);
        if (a.getname().equals(empName)) {

            System.out.println("\nPlease enter your passcode: ");

            String code = Scan.nextLine();

                if (a.check(code) == true) {
                    System.out.println("logged in");
                }
            }
        System.out.println("Employee does not exist!");
        login();

    }

我正在 for 循环中执行打印语句以查看它找到了什么,不幸的是它只是第一个帐户

编辑:我在这里包含了更多代码,在我最初的 if 语句之后我想检查用户输入的代码是否也正确。

see if the name of an employee matches one in the list then the if statement continues, further questions asked etc... it seems to only iterate once and then stop as it will only find the first user and tell me the other accounts do not exisit, even though they do!!

如果它适用于一名员工并告知其他员工不存在,那么您的 for 循环会不会 迭代一次。

您得到的输出正是代码的样子。您获得用户名 一次 然后尝试将相同的名称与列表中的每个员工匹配。如果名称相同,则要求输入密码,否则打印出该员工不存在。一切都正确,因为它在代码中。你应该在你的问题中添加预期的行为,这样我或其他人就可以在不猜测你的方法的目的的情况下修复你的代码。

这是其中一个猜测:

System.out.println("Please enter your name: ");
String empName = Scan.nextLine();
boolean userFound = false;
for (Account a : Accounts) { 
        System.out.println(a);
        if (a.getname().equals(empName)) {
            System.out.println("\nPlease enter your passcode: ");
            String code = Scan.nextLine();
                if (a.check(code) == true) {
                    System.out.println("logged in");
                    userFound = true;
                    break;
                }
            }
    }

if(userFound) {
    login();
} else {
    System.out.println("User not found.");
}

这是一个可能的解决方案,它不使用您的 Account class(因为我不知道它是什么样子)而是使用地图:

public static void main(String[] args)
{
   Scanner input = new Scanner(System.in);
   System.out.println("Hello welcome: ");
   System.out.println("Please enter your name: ");
   String empName = input.nextLine();
   boolean found = false;

   Map<String, String> accounts = new HashMap<String, String>();
   accounts.put("Geoff", "password1");
   accounts.put("Bob", "password2");
   accounts.put("John", "password3");

   Set<String> names = accounts.keySet();

   for (String a : names)
   {
      if (!a.equals(empName))
      {
         continue;
      }
      found = true;

      // three tries to login
      boolean success = false;
      for(int i = 0; i < 3; i++)
      {
         System.out.println("Please enter your passcode: ");
         String code = input.nextLine();

         if (accounts.get(a).equals(code))
         {
            System.out.println("logged in");
            success = true;
         }
         else
         {
            System.out.println("Wrong password... try again");
         }
      }
      if(!success)
      {
         System.out.println("User failed to authenticate after 3 attempts. User has been locked out!");
      }

   }
   if(!found)
   {
      System.out.println("Employee does not exist!");
   }
}

由于我不知道 login() 方法的作用,我只是将其添加到代码中。此解决方案迭代三次以尝试获取正确的密码。如果失败,将显示一条消息。