题解 | #密码强度等级#浪费时间
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String password = in.nextLine();
System.out.println(passwordLevel(password));
}
}
public static String passwordLevel(String password) {
int score = 0;
int len = password.length();
if (len <= 4) {
score = 5;
} else if (len <= 7 && len >= 5) {
score = 10;
} else {
score = 25;
}
int bigL = 0;
int smallL = 0;
int nums = 0;
int symbol = 0;
for (int i = 0; i < len; i++) {
if (isBigLetter(password.charAt(i))) {
bigL++;
} else if (isSmallLetter(password.charAt(i))) {
smallL++;
} else if (isNumber(password.charAt(i))) {
nums++;
} else {
symbol++;
}
}
//统计大小写字母和数字、符号
if (bigL >= 1 && smallL >= 1) {
score += 20;
} else if (bigL >= 1 || smallL >= 1) {
score += 10;
}
if (nums == 1) {
score += 10;
} else if (nums > 1) {
score += 20;
}
if (symbol == 1) {
score += 10;
} else if (symbol > 1) {
score += 25;
}
if (bigL > 0 && smallL > 0 && nums > 0 && symbol > 0) {
score += 5;
} else if ((bigL > 0 || smallL > 0) && nums > 0 && symbol > 0) {
score += 3;
} else if ((bigL > 0 || smallL > 0) && nums > 0) {
score += 2;
}
if (score >= 90) {
return "VERY_SECURE";
} else if (score >= 80) {
return "SECURE";
} else if (score >= 70) {
return "VERY_STRONG";
} else if (score >= 60) {
return "STRONG";
} else if (score >= 50) {
return "AVERAGE";
} else if (score >= 25) {
return "WEAK";
} else if (score >= 0) {
return "VERY_WEAK";
}
return "VERY_WEAK";
}
public static boolean isBigLetter(char c) {
if (c <= 'Z' && c >= 'A') {
return true;
}
return false;
}
public static boolean isSmallLetter(char c) {
if (c <= 'z' && c >= 'a') {
return true;
}
return false;
}
public static boolean isNumber(char c) {
if (c <= '9' && c >= '0') {
return true;
}
return false;
}
}

