简体   繁体   English

为什么我的continue语句不重新启动while循环?

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

I am trying to complete an authentication program for my final project. 我正在尝试为我的最终项目完成身份验证程序。 I am checking for user authentication, if the user info doesn't match the credentials file, the output tells them this and then prompts for username and password while increment an attemptCounter. 我正在检查用户身份验证,如果用户信息与凭据文件不匹配,输出将告诉他们这一点,然后在增加tryCounter的同时提示输入用户名和密码。 The only portion I am experiencing an issue with is when I test and incorrect attempt followed by a correct attempt, the while loop will not restart my authentication procedure, instead it simple says incorrect login again. 我遇到的唯一问题是,当我测试并尝试不正确,然后进行正确尝试时,while循环不会重新启动身份验证过程,相反,它只是简单地说了一次错误的登录。 Why isn't my continue statement restarting my while loop iteration? 为什么我的continue语句不重新启动while循环迭代?

I tired looking around for this answer on the forum and food nothing specific to my issue. 我疲倦地在论坛上四处寻找这个答案,却没有发现与我的问题有关的食物。 I tried moving my conditional statement as well to see if it was in the wrong spot for my continue to work but it fixed nothing. 我也尝试移动条件语句,以查看该语句是否在继续工作的错误位置,但没有解决任何问题。 Compiler says both of my continue statements are unnecessary. 编译器说我的两个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;
            }

Let's get rid of most of your code, let's format the code better, and let's leave only the control structures to see what the continue statements are doing: 让我们摆脱大多数代码,让代码更好地格式化,让我们只保留控件结构,看看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) ****
        }
    }
}

If line (A) is reached, you're in the if (roll == null) block, the else will never be entered, and the while loop will repeat regardless of the continue statement making continue unnecessary and distracting. 如果到达(A)行,则位于if (roll == null)块中,将永远不会输入else ,并且while循环将重复, 而无论continue语句是否使continue不必要和分散注意力。

Likewise if line (B) is reached, you're in the last control block of the while loop, and so the loop will continue regardless of the continue statement making continue unnecessary and distracting. 同样,如果到达(B)行,则您处于while循环的最后一个控制块中,因此该循环将继续进行, 而与继续语句无关,从而使continue不必要和分散注意力。

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

相关问题 java While循环语句将继续循环,但不会包含我的退出循环的语句 - java While-loop statement will continue to loop but won't include my statement to exit the loop 为什么我的 java while 循环继续以真实状态打印? - Why is my java while loop continue printing with true status? 为什么while循环中的if语句仅在调试中执行? - Why does my if statement in my while loop only execute in debugging? 为什么我的 While 循环不会像我的 if else 语句那样循环我的 Catch? - Why is my While Loop wont loop my Catch, like my if else statement? 为什么我的打印语句在一段时间循环后没有产生 output? - Why is my print statement producing no output after a while loop? Java为什么在While循环中跳过if语句? - Why Does Java Skip My If Statement in a While Loop? 为什么if语句发送的while循环继续出错 - why does if statement send continue to wrong while loop 为什么每次继续while循环时都会重置变量? - Why are my variables being reset every time I continue a while loop? 我的打印语句不会在我的 while 循环中打印我的变量 - My print statement wont print my variable in my while loop 如果不满足if语句中的条件,为什么while循环中的else语句不执行? - Why won't the else statement in my while loop execute when the conditions in the if statement aren't met?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM