Java 为什么continue语句不重新启动while循环?
我正在为我的最终项目完成一个身份验证程序。我正在检查用户身份验证,如果用户信息与凭据文件不匹配,输出会告诉他们这一点,然后在递增尝试计数器时提示输入用户名和密码。我遇到问题的唯一部分是,当我测试错误的尝试,然后是正确的尝试时,while循环不会重新启动我的身份验证过程,而是简单地说再次登录错误。为什么continue语句不重新启动while循环迭代 我厌倦了在论坛上四处寻找这个答案,也没有任何与我的问题相关的东西。我也试着移动我的条件语句,看看它是否在我继续工作的错误位置,但它没有解决任何问题。编译器说我的两个continue语句都是不必要的Java 为什么continue语句不重新启动while循环?,java,Java,我正在为我的最终项目完成一个身份验证程序。我正在检查用户身份验证,如果用户信息与凭据文件不匹配,输出会告诉他们这一点,然后在递增尝试计数器时提示输入用户名和密码。我遇到问题的唯一部分是,当我测试错误的尝试,然后是正确的尝试时,while循环不会重新启动我的身份验证过程,而是简单地说再次登录错误。为什么continue语句不重新启动while循环迭代 我厌倦了在论坛上四处寻找这个答案,也没有任何与我的问题相关的东西。我也试着移动我的条件语句,看看它是否在我继续工作的错误位置,但它没有解决任何问题。
//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;
}
//提示用户输入用户名
System.out.println(“请输入您的用户名(\'L\'注销\'Q\'退出):”;
username=scnr.nextLine();
//评估用户是否要立即退出
if(username.equals(“Q”)){
系统出口(0);
}
//提示用户输入密码并存储到密码字段
System.out.println(“请输入您的密码:”);
password=scnr.nextLine();
系统输出打印(“\n”);
//while循环,其中包含身份验证和授权逻辑
而(!username.equals(“Q”)&&attemptCounter<2){
//调用MD5Hash类的hashInput方法返回经过哈希处理的用户密码
userHashedPassword=userHash.hashInput(密码);
//while循环以打开凭据进行身份验证比较
while(cfScnr.hasNextLine()){
//将下一行扫描的文件分配给字段进行比较
line=cfScnr.nextLine();
//用于确定行中是否包含用户名和密码的条件语句
//一旦行中包含用户的用户名和密码,就会中断文件循环
//用于返回角色字符串并删除额外字符和空白的语句逻辑
if(line.contains(用户名)&&line.contains(userHashedPassword)){
dqLocation=line.lastIndexOf(“”);
角色=行。子字符串(dqLocation);
role=role.replaceAll(\\s+,“”);
role=role.replace(“\”,”);
打破
}
}
//条件语句,用于确定前一个循环条件语句的含义
//如果不是,此条件将通知用户错误的用户名和/或密码
//通知他们剩余的尝试,并提示他们输入新用户名和密码
//跟踪尝试并确定他们想要退出。如果未输入Q,则主while循环将重新启动身份验证
如果(角色==null){
尝试计数器++;
System.out.println(“用户名或密码不正确。”+(3-attemptCounter)+“剩余尝试次数”);
System.out.println(“请输入您的用户名(\'L\'注销\'Q\'退出):”;
username=scnr.nextLine();
if(username.equals(“Q”)){
系统出口(0);
}
System.out.println(“请输入您的密码:”);
password=scnr.nextLine();
继续;
}
//此条件语句仅在用户经过身份验证时运行
否则{
//创建新文件对象和扫描程序对象以扫描角色文件
File rFile=新文件(“src\\zooauthenticationsystem\\”+role+“.txt”);
扫描仪rfScnr=新扫描仪(rFile);
//while循环解析角色文件并将文件的行输出到控制台
while(rfScnr.hasNextLine()){
rolePrint=rfScnr.nextLine();
系统输出打印LN(角色打印);
}
//提示用户是否要注销或只是退出程序
System.out.println(“\n按“L”退出,按“Q”退出。”);
userDecision=scnr.nextLine();
//条件语句来确定它们的输入,并将角色重置为null来重置身份验证循环条件语句,从而重新启动main while循环
if(userDecision.equals(“L”)){
System.out.println(“请输入您的用户名:”);
username=scnr.nextLine();
if(username.equals(“Q”)){
系统出口(0);
}
System.out.println(“请输入您的密码:”);
password=scnr.nextLine();
系统输出打印(“\n”);
role=null;
继续;
}
让我们去掉大部分代码,让我们更好地格式化代码,只留下控制结构,看看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) ****
}
}
}
while(!username.equals(“Q”)&&attemptCounter<2){
userHashedPassword=userHash.hashInput(密码);
while(cfScnr.hasNextLine()){
line=cfScnr.nextLine();
if(line.contains(用户名)&&line.contains(userHashedPassword)){
//…做点什么
打破
}
}
如果(角色==null){
//…做点什么
继续;//**(A)****
}否则{
//…做点什么
if(userDecision.equals(“L”)){
//…做点什么
继续;//**(B)****
}
}
}
如果到达第(A)行,则您处于If(roll==null)
块中,将永远不会输入else
,而while循环将重复,无论continue语句如何都会使continue
变得不必要和分散注意力
同样,如果到达第(B)行,则您处于while lo的最后一个控制块中