简体   繁体   English

Java for循环使用if语句仅迭代一次

[英]Java for loop with if statement only iterating once

I have a version of a login for an employee system i would like to make, I have a for loop which should go through the entire list of Accounts, then 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循环,该循环应遍历整个帐户列表,然后查看雇员名称是否与列表中的一个匹配,然后if语句继续,再问其他问题,等等。。。它似乎只迭代一次,然后停止,因为它只会找到第一个用户,并告诉我其他帐户不存在,即使它们存在!! What am i doing wrong? 我究竟做错了什么? Also my list contains Employees and Managers which inherit from Account, the if statement uses the getName in Account to compare if it equals to the user input. 我的列表还包含从Account继承的雇员和经理,if语句使用Account中的getName来比较它是否等于用户输入。 Sorry if this is ridiculously stupid/bad! 抱歉,这很荒谬/糟糕! thanks. 谢谢。

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

Here is where i populate my Account, the main method calls this and the list() is called whihc contains the problematic loop 这是我填充帐户的地方,主要方法调用此方法,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();

    }

problem: 问题:

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();

    }

I am doing the print statement in the for loop to see what it is findng, and unfortunalty it is only the first account 我正在for循环中执行print语句,以查看它的内容,但不幸的是,这只是第一个帐户

EDIT: I have included more code here, my after my initial if statement i want to check if the code the user enters is also correct. 编辑:我在这里包括更多的代码,我最初的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!! 看看雇员的姓名是否与列表中的一个匹配,然后if语句继续,进一步的问题等等。。。它似乎只重复一次然后停止,因为它只会找到第一个用户,告诉我其他帐户不退出,即使他们这样做!!

If it works for one employee and tells that others don't exist then your for loop does not iterate once. 它是否适合一个员工,并告诉其他人不存在,那么你的for循环重复一次。

The output you get is exactly what the code looks like. 您得到的输出正是代码的样子。 You get username once then try to match the same name with every employee in the list. 一次获得用户名然后尝试将相同的名称与列表中的每个员工匹配。 If the names are equal you ask for password, otherwise you print out that employee doesn't exist. 如果名称相同,则要求输入密码,否则您将打印出该员工不存在。 Everything right as it is in the code. 一切都正确,如代码中所示。 You should add to your question the expected behaviour so I, or someone else can fix your code without guessing the purpose of your methods. 您应该在问题中添加预期的行为,以便我或其他人可以在不猜测方法目的的情况下修复代码。

Here's one of those guesses: 这是这些猜测之一:

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.");
}

This is a possible solution that doesn't use your Account class (since I do not know what it looks like) and instead uses a Map: 这是一个可能的解决方案,它不使用您的Account类(因为我不知道它的外观),而是使用Map:

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!");
   }
}

Since I do not know what the login() method does, I just simply added that into the code. 由于我不知道login()方法的作用,因此我只是将其添加到了代码中。 This solution iterates three times in an attempt to get the correct password. 此解决方案反复尝试三次以获取正确的密码。 If that fails, a message is displayed. 如果失败,则会显示一条消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM