Java编程:输入无效密码时如何终止程序或重新输入?
密码验证代码修复方案
你的核心问题是无论密码是否有效,程序都会执行登录详情打印逻辑,以下提供两种符合你需求的修复方案:
方案一:密码无效时直接终止程序
将打印登录详情的代码移到密码有效的分支中,无效时输出错误提示后终止程序:
final int MAX_LENGTH = 8; final int MIN_UPPERCASE = 1; final int MIN_DIGITS = 1; final int MIN_SPECIAL = 1; Scanner sc = new Scanner(System.in); // 收集用户基本信息 System.out.println("Enter Name: "); String name = sc.nextLine(); int spacePosition = name.indexOf(" "); System.out.println("\nEnter Birthday (MM DD YYYY): "); String birthday = sc.nextLine(); // 密码验证逻辑 System.out.println("\nEnter password: "); String password = sc.nextLine(); int uppercaseCounter = 0; int digitCounter = 0; int specialCounter = 0; int spaceCounter = 0; // 遍历密码字符统计各类字符数量 for (int i = 0; i < password.length(); i++) { char c = password.charAt(i); if (Character.isUpperCase(c)) { uppercaseCounter++; } if (c == ' ') { spaceCounter++; } if (c == '$' || c == '#' || c == '?' || c == '!' || c == '_' || c == '=' || c == '%' || c == '.') { specialCounter++; } else if (Character.isDigit(c)) { digitCounter++; } } // 判断密码是否有效 boolean isPasswordValid = password.length() >= MAX_LENGTH && uppercaseCounter >= MIN_UPPERCASE && specialCounter >= MIN_SPECIAL && digitCounter >= MIN_DIGITS && spaceCounter == 0; // 新增空格校验,原代码未在有效判断中添加 if (isPasswordValid) { // 密码有效,打印登录详情 System.out.println("\nYour login details"); System.out.println("Name : " + name); System.out.println("Birthday: " + birthday); System.out.println("Username: " + name.substring(0, 2) + name.substring(spacePosition + 1) + birthday.substring(3, 5)); System.out.println("Password: " + password); } else { // 输出错误提示 if (password.length() < MAX_LENGTH) { System.out.println("Enter at least 8 characters"); } if (spaceCounter > 0) { System.out.println("Password contains whitespaces"); } if (uppercaseCounter < MIN_UPPERCASE) { System.out.println("Enter at least 1 Uppercase Character"); } if (digitCounter < MIN_DIGITS) { System.out.println("Enter at least 1 digit"); } if (specialCounter < MIN_SPECIAL) { System.out.println("Enter at least 1 special character"); } // 终止程序 System.exit(1); } sc.close();
方案二:循环要求用户输入直到密码有效
如果希望用户输入无效密码后重新输入,而非直接终止,可将密码输入和验证逻辑放到循环中,每次循环重置计数器:
final int MAX_LENGTH = 8; final int MIN_UPPERCASE = 1; final int MIN_DIGITS = 1; final int MIN_SPECIAL = 1; Scanner sc = new Scanner(System.in); // 收集用户基本信息 System.out.println("Enter Name: "); String name = sc.nextLine(); int spacePosition = name.indexOf(" "); System.out.println("\nEnter Birthday (MM DD YYYY): "); String birthday = sc.nextLine(); String password = ""; boolean isPasswordValid = false; // 循环直到输入有效密码 while (!isPasswordValid) { System.out.println("\nEnter password: "); password = sc.nextLine(); // 每次循环重置计数器,避免上次输入的计数影响 int uppercaseCounter = 0; int digitCounter = 0; int specialCounter = 0; int spaceCounter = 0; // 遍历密码字符统计各类字符数量 for (int i = 0; i < password.length(); i++) { char c = password.charAt(i); if (Character.isUpperCase(c)) { uppercaseCounter++; } if (c == ' ') { spaceCounter++; } if (c == '$' || c == '#' || c == '?' || c == '!' || c == '_' || c == '=' || c == '%' || c == '.') { specialCounter++; } else if (Character.isDigit(c)) { digitCounter++; } } // 判断密码是否有效 isPasswordValid = password.length() >= MAX_LENGTH && uppercaseCounter >= MIN_UPPERCASE && specialCounter >= MIN_SPECIAL && digitCounter >= MIN_DIGITS && spaceCounter == 0; if (!isPasswordValid) { // 输出错误提示 if (password.length() < MAX_LENGTH) { System.out.println("Enter at least 8 characters"); } if (spaceCounter > 0) { System.out.println("Password contains whitespaces"); } if (uppercaseCounter < MIN_UPPERCASE) { System.out.println("Enter at least 1 Uppercase Character"); } if (digitCounter < MIN_DIGITS) { System.out.println("Enter at least 1 digit"); } if (specialCounter < MIN_SPECIAL) { System.out.println("Enter at least 1 special character"); } System.out.println("Please try again."); } } // 密码有效,打印登录详情 System.out.println("\nYour login details"); System.out.println("Name : " + name); System.out.println("Birthday: " + birthday); System.out.println("Username: " + name.substring(0, 2) + name.substring(spacePosition + 1) + birthday.substring(3, 5)); System.out.println("Password: " + password); sc.close();
额外修复说明
- 移除了重复创建的
Scanner input,只用一个sc即可处理所有输入 - 修正了原代码中密码有效判断的漏洞:新增
spaceCounter == 0校验,避免含空格的密码被判定为有效 - 调整了变量命名(如
MAX改为MAX_LENGTH),提升代码可读性 - 修正了原代码中
name.substring(spacePosition + 3)的索引越界风险,改为spacePosition + 1(适配常规的"名 姓"格式)
内容的提问来源于stack exchange,提问作者Amimir
相关产品推荐
相关产品推荐

