题解 | #密码强度等级#--流程控制
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sum = 0;
while (in.hasNextLine()) {
String str = in.nextLine();
char[] chrs = str.toCharArray();
int len = str.length();
int lowLetterCount = 0;
int upLetterCount = 0;
int numCount = 0;
int otherCount = 0;
for (char chr : chrs) {
if (Character.isDigit(chr)) {
numCount++;
} else if (Character.isLetter(chr) && Character.isLowerCase(chr)) {
lowLetterCount++;
} else if (Character.isLetter(chr) && Character.isUpperCase(chr)) {
upLetterCount++;
} else {
otherCount++;
}
}
// 密码长度得分
if (len <= 4) {
sum += 5;
} else if (len >= 8) {
sum += 25;
} else {
sum += 10;
}
// 字母得分
if (lowLetterCount == 0 || upLetterCount == 0) {
sum += 10;
}
if (lowLetterCount > 0 && upLetterCount > 0) {
sum += 20;
}
// 数字得分
if (numCount == 1) {
sum += 10;
}
if (numCount > 1) {
sum += 20;
}
// 符号得分
if (otherCount == 1) {
sum += 10;
}
if (otherCount > 1) {
sum += 25;
}
// 奖励得分
if (numCount > 0 && (lowLetterCount > 0 || upLetterCount > 0) &&
otherCount == 0) {
sum += 2;
}
if (numCount > 0 && otherCount > 0 &&
Integer.min(lowLetterCount, upLetterCount) == 0 &&
Integer.max(lowLetterCount, upLetterCount) > 0) {
sum += 3;
}
if (numCount > 0 && otherCount > 0 && lowLetterCount > 0 && upLetterCount > 0) {
sum += 5;
}
}
score(sum);
}
private static void score(int sum) {
if (sum >= 90) {
System.out.println("VERY_SECURE");
} else if (sum >= 80 && sum < 90) {
System.out.println("SECURE");
} else if (sum >= 70 && sum < 80) {
System.out.println("VERY_STRONG");
} else if (sum >= 60 && sum < 70) {
System.out.println("STRONG");
} else if (sum >= 50 && sum < 60) {
System.out.println("AVERAGE");
} else if (sum >= 25 && sum < 50) {
System.out.println("WEAK");
} else {
System.out.println("VERY_WEAK");
}
}
}
查看3道真题和解析