题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static int calculate(String password) { int score = 0; // 密码长度 if (password.length() <= 4) { score += 5; } else if (password.length() >= 5 && password.length() <= 7) { score += 10; } else { score += 25; } // 包含字母,没有字母0分 boolean lowerCase = false; boolean upperCase = false; int digitCount = 0; int symbolCount = 0; for (int i = 0; i < password.length(); i++) { char c = password.charAt(i); if (Character.isLowerCase(c)) { lowerCase = true; } else if (Character.isUpperCase(c)) { upperCase = true; } else if (Character.isDigit(c)) { digitCount++; } else if (c >= 32 && c <= 47 || c >= 58 && c <= 64 || c >= 91 && c <= 96 || c >= 123 && c <= 126) { symbolCount++; } } if (lowerCase && !upperCase || !lowerCase && upperCase) { score += 10; } else if (lowerCase && upperCase) { score += 20; } // 数字 if (digitCount == 1) { score += 10; } else if (digitCount > 1) { score += 20; } // 符号 if (symbolCount == 1) { score += 10; } else if (symbolCount > 1) { score += 25; } // 奖励项 boolean flag1 = (lowerCase || upperCase) && digitCount > 0; boolean flag2 = flag1 && symbolCount > 0; boolean flag3 = (lowerCase && upperCase) && digitCount > 0 && symbolCount > 0; if (flag3) { score += 5; return score; } if (flag2) { score += 3; return score; } if (flag1) { score += 2; return score; } return score; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String password = sc.next(); int score = calculate(password); if (score >= 90) { System.out.println("VERY_SECURE"); } else if (score >= 80) { System.out.println("SECURE"); } else if (score >= 70) { System.out.println("VERY_STRONG"); } else if (score >= 60) { System.out.println("STRONG"); } else if (score >= 50) { System.out.println("AVERAGE"); } else if (score >= 25) { System.out.println("WEAK"); } else if (score >= 0) { System.out.println("VERY_WEAK"); } } }