题解 | 密码强度等级
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
// 计算密码强度等级
private static String calculateSecurityLevel(String password) {
boolean hasUpperCase = false;
boolean hasLowerCase = false;
int digitCount = 0;
int specialCharCount = 0;
// 遍历密码字符,统计各类型字符
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
hasUpperCase = true;
} else if (Character.isLowerCase(c)) {
hasLowerCase = true;
} else if (Character.isDigit(c)) {
digitCount++;
} else if (isSpecialChar(c)) {
specialCharCount++;
}
}
// 计算各部分的得分
int lengthScore = calculateLengthScore(password.length());
int letterScore = calculateLetterScore(hasUpperCase, hasLowerCase);
int digitScore = calculateDigitScore(digitCount);
int specialCharScore = calculateSpecialCharScore(specialCharCount);
int bonusScore = calculateBonusScore(hasUpperCase, hasLowerCase, digitCount,
specialCharCount);
int total = lengthScore + letterScore + digitScore + specialCharScore +
bonusScore;
return determineLevel(total);
}
// 计算字母得分
private static int calculateLetterScore(boolean hasUpperCase,
boolean hasLowerCase) {
if (!hasUpperCase && !hasLowerCase) return 0;
else if (hasUpperCase && hasLowerCase) return 20;
else return 10;
}
// 计算奖励得分
private static int calculateBonusScore(boolean hasUpperCase,
boolean hasLowerCase, int digitCount, int specialCharCount) {
boolean hasBothCase = hasUpperCase && hasLowerCase;
boolean hasLetters = hasUpperCase || hasLowerCase;
boolean hasDigits = digitCount > 0;
boolean hasSpecialChar = specialCharCount > 0;
if (hasBothCase && hasDigits && hasSpecialChar) return 5;
else if (hasLetters && hasDigits && hasSpecialChar) return 3;
else if (hasLetters && hasDigits) return 2;
else return 0;
}
// 计算特殊字符得分
private static int calculateSpecialCharScore(int specialCharCount) {
if (specialCharCount == 0) return 0;
else if (specialCharCount == 1) return 10;
else return 25;
}
// 计算数字得分
private static int calculateDigitScore(int digitCount) {
if (digitCount == 0) return 0;
else if (digitCount == 1) return 10;
else return 20;
}
// 计算密码长度得分
private static int calculateLengthScore(int length) {
if (length <= 4) return 5;
else if (length <= 7) return 10;
else return 25;
}
//判断是否为特殊字符
private static boolean isSpecialChar(char c) {
int code = c;
return (code >= 0x21 && code <= 0x2F) || // !"#$%&'()*+,-./
(code >= 0x3A && code <= 0x40) || // :;<=>?@
(code >= 0x5B && code <= 0x60) || // [\]^_`
(code >= 0x7B && code <= 0x7E); // {|}~
}
// 根据密码强度值,返回对应的等级
private static String determineLevel(int total) {
if (total >= 90) return "VERY_SECURE";
else if (total >= 80) return "SECURE";
else if (total >= 70) return "VERY_STRONG";
else if (total >= 60) return "STRONG";
else if (total >= 50) return "AVERAGE";
else if (total >= 25) return "WEAK";
else return "VERY_WEAK";
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String password = in.nextLine();
System.out.println(calculateSecurityLevel(password));
}
}
}
查看10道真题和解析