题解 | 密码强度等级
密码强度等级
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);
while (in.hasNext()) {
String pwd = in.nextLine();
int numCount = 0;
int upCharCount = 0;
int lowCharCount = 0;
int zfCount = 0;
for (int i = 0; i < pwd.length(); i++) {
char c = pwd.charAt(i);
if (Character.isDigit(c)) {
numCount++;
continue;
}
if (c >= 'a' && c <= 'z') {
lowCharCount = 1;
continue;
}
if (c >= 'A' && c <= 'Z') {
upCharCount = 1;
continue;
}
zfCount++;
}
int score = 0;
if (pwd.length() <= 4) {
score += 5;
} else if (pwd.length() <= 7) {
score += 10;
} else {
score += 25;
}
if (lowCharCount + upCharCount == 1) {
score += 10;
} else if (lowCharCount + upCharCount == 2) {
score += 20;
}
if (numCount == 1) {
score += 10;
}
if (numCount > 1) {
score += 20;
}
if (zfCount == 1) {
score += 10;
}
if (zfCount > 1) {
score += 25;
}
if ((upCharCount + lowCharCount) == 2 && numCount > 0 && zfCount >= 0) {
score += 5;
} else if ((upCharCount + lowCharCount) == 1 && numCount > 0 && zfCount >= 0) {
score += 3;
} else if ((upCharCount + lowCharCount) == 1 && numCount > 0) {
score += 2;
}
if (score >= 90) {
System.out.println("VERY_SECURE");
break;
}
if (score >= 80) {
System.out.println("SECURE");
break;
}
if (score >= 70) {
System.out.println("VERY_STRONG");
break;
}
if (score >= 60) {
System.out.println("STRONG");
break;
}
if (score >= 50) {
System.out.println("AVERAGE");
break;
}
if (score >= 25) {
System.out.println("WEAK");
break;
}
if (score >= 0) {
System.out.println("VERY_WEAK");
break;
}
}
}
}

查看13道真题和解析