簡體   English   中英

為什么我的continue語句不重新啟動while循環?

[英]Why will my continue statement not restart my while loop?

我正在嘗試為我的最終項目完成身份驗證程序。 我正在檢查用戶身份驗證,如果用戶信息與憑據文件不匹配,輸出將告訴他們這一點,然后在增加tryCounter的同時提示輸入用戶名和密碼。 我遇到的唯一問題是,當我測試並嘗試不正確,然后進行正確嘗試時,while循環不會重新啟動身份驗證過程,相反,它只是簡單地說了一次錯誤的登錄。 為什么我的continue語句不重新啟動while循環迭代?

我疲倦地在論壇上四處尋找這個答案,卻沒有發現與我的問題有關的食物。 我也嘗試移動條件語句,以查看該語句是否在繼續工作的錯誤位置,但沒有解決任何問題。 編譯器說我的兩個continue語句都是不必要的。

    //prompting user for username
    System.out.println("Please enter your username (\"L\" to logout \"Q\" to quit): ");
    username = scnr.nextLine();
    //evaluating if user wants to quit immediately 
    if(username.equals("Q")) {
        System.exit(0);
    }
    //prompting user for password and storing to password field
    System.out.println("Please enter your password: ");
    password = scnr.nextLine();
    System.out.print("\n");

    //while loop that contains the authentication and authorization logic 
    while (!username.equals("Q") && attemptCounter < 2){
        //calling of hashInput method of MD5Hash class to return the hashed user password
        userHashedPassword = userHash.hashInput(password);
        //while loop to open the credentials for authentication comparison
        while (cfScnr.hasNextLine()) {
            //assigning the files scanned next line to a field for comparison
            line = cfScnr.nextLine();
            //conditional statement to determine if username and password are contained on the line
            //will break file loop as soon as line contains the user's username and password
            //statement logic used to return the role string and remove extra characters and white space
            if (line.contains(username) && line.contains(userHashedPassword)) {
                dqLocation = line.lastIndexOf('"');
                role = line.substring(dqLocation);
                role = role.replaceAll("\\s+", "");
                role = role.replace("\"", "");
                break;
            }
        }
        //conditional statement used to determine if previous loops condtional statement was meant
        //if it wasn't this condition will inform the user of incorrect username and/or password
        //inform them of attempts remaining and prompt them for a new username and password while
        //tracking the attempts and it they want to quit. If Q isn't entered main while loop will restart authentication
        if (role == null){
            attemptCounter++;
            System.out.println("Username or password incorrect. " + (3 - attemptCounter) + " attempts remaining.");
            System.out.println("Please enter your username (\"L\" to logout \"Q\" to quit): ");
            username = scnr.nextLine();
            if(username.equals("Q")) {
                System.exit(0);
            }
            System.out.println("Please enter your password: ");
            password = scnr.nextLine();
            continue;
            }
        //this conditional statement runs only when the user is authenticated
        else {
            //creating new file object and scanner object to scan the role file
            File rFile = new File("src\\zooauthenticationsystem\\" + role + ".txt");
            Scanner rfScnr = new Scanner(rFile);
            //while loop to parse through the role file and output the lines of the file to the console
            while (rfScnr.hasNextLine()){
                rolePrint = rfScnr.nextLine();
                System.out.println(rolePrint);
            }
            //prompting user if they would like to logout or simply quit the program
            System.out.println("\nPress \"L\" to logout and \"Q\" to quit.");
            userDecision = scnr.nextLine();
            //conditional statement to determine their input, and resetting role to null to reset authentication loop conditional statements, restarts main while loop
            if (userDecision.equals("L")){
                System.out.println("Please enter your username: ");
                username = scnr.nextLine();
                if(username.equals("Q")) {
                    System.exit(0);
                }
                System.out.println("Please enter your password: ");
                password = scnr.nextLine();
                System.out.print("\n");
                role = null;
                continue;
            }

讓我們擺脫大多數代碼,讓代碼更好地格式化,讓我們只保留控件結構,看看continue語句在做什么:

while (!username.equals("Q") && attemptCounter < 2) {
    userHashedPassword = userHash.hashInput(password);
    while (cfScnr.hasNextLine()) {
        line = cfScnr.nextLine();
        if (line.contains(username) && line.contains(userHashedPassword)) {
            // ... do some stuff
            break;
        }
    }
    if (role == null) {
        // ... do some stuff
        continue;  // **** (A) ****
    } else {
        // ... do some stuff
        if (userDecision.equals("L")){
            // ... do some stuff
            continue;  // **** (B) ****
        }
    }
}

如果到達(A)行,則位於if (roll == null)塊中,將永遠不會輸入else ,並且while循環將重復, 而無論continue語句是否使continue不必要和分散注意力。

同樣,如果到達(B)行,則您處於while循環的最后一個控制塊中,因此該循環將繼續進行, 而與繼續語句無關,從而使continue不必要和分散注意力。

暫無
暫無

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

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