我正在尝试为我的最终项目完成身份验证程序。我正在检查用户身份验证,如果用户信息与凭据文件不匹配,输出将告诉他们这一点,然后在增加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;
}
答案 0 :(得分:1)
让我们摆脱大部分代码,让我们更好地格式化代码,让我们只保留控制结构以查看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语句为何使continue
不再必要和分散注意力。