java - 为什么我的 continue 语句不会重新启动我的 while 循环?
问题描述
我正在尝试为我的最终项目完成一个身份验证程序。我正在检查用户身份验证,如果用户信息与凭据文件不匹配,输出会告诉他们这一点,然后提示输入用户名和密码,同时增加一个尝试计数器。我遇到问题的唯一部分是当我测试和错误尝试之后是正确尝试时,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
永远不会进入,并且无论 continue 语句如何, while 循环都会重复,从而变得continue
不必要和分散注意力。
同样,如果到达 (B) 行,则您处于 while 循环的最后一个控制块中,因此无论 continue 语句是否continue
不必要和分散注意力,循环都将继续。
推荐阅读
- r - 对于新的“ggproto”,需要使用“extra_params”指定哪些参数
- java - 如何在执行前验证 Spark 表达式语法
- javascript - 角度切换按钮逻辑
- php - laravel 8 图像验证无法正常工作
- bash - Bash:进程块 b/c 读取循环有时无法从文件描述符中读取更多数据,但手动 cat /proc/
/fd/ 能够 - azure - 从门户部署逻辑应用时无法在存储帐户中创建文件共享
- c# - 如何检测媒体中的特定图片是否被任何页面使用
- gitlab - 如何使用 Gitlab CI 测试数据库迁移
- flutter - 更改 makeInput 标签文本颜色
- xero-api - 建立连接后在 Xero 上获取用户电子邮件