题解 | 密码强度等级
密码强度等级
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);
String password = in.nextLine();
int score = 0;
if (password.length() <= 4) {
score += 5;
} else if (password.length() >= 5 && password.length() <= 7) {
score += 10;
} else {
score += 25;
}
char[] chars = password.toCharArray();
int digit = 0;
int bigLetter = 0;
int smallLetter = 0;
int other = 0;
for (char c : chars) {
if (Character.isDigit(c)) {
digit++;
} else if (Character.isLowerCase(c)) {
smallLetter++;
} else if (Character.isUpperCase(c)) {
bigLetter++;
} else {
other++;
}
}
if (smallLetter == 0 && bigLetter == 0) {
score += 0;
} else if ((smallLetter == 0 && bigLetter > 0) || (smallLetter > 0 &&
bigLetter == 0)) {
score += 10;
} else if (smallLetter > 0 && bigLetter > 0) {
score += 20;
}
if (digit == 0) {
score += 0;
} else if (digit == 1) {
score += 10;
} else {
score += 20;
}
if (other == 0) {
score += 0;
} else if (other == 1) {
score += 10;
} else {
score += 25;
}
if (smallLetter > 0 && bigLetter > 0 && digit > 0 && other > 0) {
score += 5;
} else if (digit > 0 && other > 0 && (smallLetter > 0 || bigLetter > 0)) {
score += 3;
} else if (digit > 0 && (smallLetter > 0 || bigLetter > 0)) {
score += 2;
}
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");
}
}
}