Java带3次密码尝试限制的登录程序逻辑错误修复求助
Java密码验证程序修复:正确登录后仍输出账号封禁提示的问题
问题概述
开发的Java密码验证程序,用于对比存储密码与输入密码完成验证,设置了3次密码尝试限制。但存在逻辑错误:输入正确密码完成操作后,仍会额外输出Account is blocked. Contact Bank manager提示,尝试添加break语句未彻底解决问题。
原代码
public class Test { int otherBankUserId=345; otherBankUserPass="suresh"; int otherAcc;String otherPass=null; Scanner sc = new Scanner(System.in); public void checkOtherPass() { System.out.println("Enter the account"); otherAcc = sc.nextInt(); if(otherAcc==otherBankUserId) { while(passError<3) { System.out.println("Account number is verified. Enter the password"); otherPass = sc.next(); if(otherPass.equals(otherBankUserPass)) { System.out.println("logged in successfully"); System.out.println("Enter the amount:"); int amountSum=sc.nextInt(); System.out.println(amountSum+"Rs withdraw successfully done. Remove the card!"); } else { System.out.println("wrong password!"); passError=passError+1; System.out.println("Wrong Entries:"+passError+"\nMax Wrong pass entries:3"); } } System.out.println("Account is blocked. Contact Bank manager"); } else { System.out.println("Wrong account number.Do the process again"); } } public static void main(String[] args) { Test t = new Test(); t.checkOtherPass(); } }
原运行输出
Enter the account 345 Account number is verified. Enter the password suresh logged in successfully Enter the amount: 333 333 withdraw successfully done. Remove the card! Account is blocked. Contact Bank manager
问题分析
- 编译级错误:
passError变量未声明初始化,会导致编译失败otherBankUserPass缺少String类型声明,不符合Java语法规范
- 核心逻辑错误:
- 密码验证成功后未跳出
while循环,循环会继续执行到错误次数达3次 - 循环结束后无条件输出封禁提示,忽略了用户已成功登录的情况
- 密码验证成功后未跳出
修复后的代码
import java.util.Scanner; public class Test { int otherBankUserId = 345; String otherBankUserPass = "suresh"; // 补全String类型声明 int otherAcc; String otherPass = null; Scanner sc = new Scanner(System.in); int passError = 0; // 初始化密码错误次数 public void checkOtherPass() { System.out.println("Enter the account"); otherAcc = sc.nextInt(); if (otherAcc == otherBankUserId) { boolean isLoggedIn = false; // 标记用户是否成功登录 while (passError < 3) { System.out.println("Account number is verified. Enter the password"); otherPass = sc.next(); if (otherPass.equals(otherBankUserPass)) { System.out.println("logged in successfully"); System.out.println("Enter the amount:"); int amountSum = sc.nextInt(); System.out.println(amountSum + "Rs withdraw successfully done. Remove the card!"); isLoggedIn = true; // 标记登录成功 break; // 跳出循环,终止后续验证 } else { passError++; System.out.println("wrong password!"); System.out.println("Wrong Entries:" + passError + "\nMax Wrong pass entries:3"); } } // 仅当未登录且错误次数耗尽时,输出封禁提示 if (!isLoggedIn && passError >= 3) { System.out.println("Account is blocked. Contact Bank manager"); } } else { System.out.println("Wrong account number.Do the process again"); } } public static void main(String[] args) { Test t = new Test(); t.checkOtherPass(); } }
修改说明
- 补全变量的类型声明与初始化,解决编译错误
- 添加
isLoggedIn布尔变量,用于追踪用户登录状态 - 密码验证成功后,设置登录标记并通过
break跳出循环,避免无效的后续验证 - 调整封禁提示的输出逻辑:仅当用户未成功登录且错误次数达到3次时,才输出该提示
内容的提问来源于stack exchange,提问作者Afzal Hamdulay
相关产品推荐
相关产品推荐

