簡體   English   中英

Java for循環使用if語句僅迭代一次

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

我要創建一個用於雇員系統的登錄版本,我有一個for循環,該循環應遍歷整個帳戶列表,然后查看雇員名稱是否與列表中的一個匹配,然后if語句繼續,再問其他問題,等等。。。它似乎只迭代一次,然后停止,因為它只會找到第一個用戶,並告訴我其他帳戶不存在,即使它們存在!! 我究竟做錯了什么? 我的列表還包含從Account繼承的雇員和經理,if語句使用Account中的getName來比較它是否等於用戶輸入。 抱歉,這很荒謬/糟糕! 謝謝。

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循環中執行print語句,以查看它的內容,但不幸的是,這只是第一個帳戶

編輯:我在這里包括更多的代碼,我最初的if語句后,我想檢查用戶輸入的代碼是否正確。

看看雇員的姓名是否與列表中的一個匹配,然后if語句繼續,進一步的問題等等。。。它似乎只重復一次然后停止,因為它只會找到第一個用戶,告訴我其他帳戶不退出,即使他們這樣做!!

它是否適合一個員工,並告訴其他人不存在,那么你的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類(因為我不知道它的外觀),而是使用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!");
   }
}

由於我不知道login()方法的作用,因此我只是將其添加到了代碼中。 此解決方案反復嘗試三次以獲取正確的密碼。 如果失敗,則會顯示一條消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM