题解 | #密码强度等级#
密码强度等级
http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String n = sc.nextLine();
char[] chars = n.toCharArray();
int sum = getSum(chars);
String res = getRes(sum);
System.out.println(res);
}
}
private static String getRes(int sum) {
if (sum >= 90) {
return "VERY_SECURE";
}
if (sum >= 80) {
return "SECURE";
}
if (sum >= 70) {
return "VERY_STRONG";
}
if (sum >= 60) {
return "STRONG";
}
if (sum >= 50) {
return "AVERAGE";
}
if (sum >= 25) {
return "WEAK";
} else {
return "VERY_WEAK";
}
}
private static int getSum(char[] chars) {
int countN = 0, countL = 0, countU = 0, countO = 0;
int countW = 0;
int sum = 0;
//长度
if (chars.length <= 4) {
sum += 5;
} else if (chars.length >= 8) {
sum += 25;
} else {
sum += 10;
}
//字母
for (int i = 0; i < chars.length; i++) {
if (Character.isLetter(chars[i])) {
if (Character.isLowerCase(chars[i])) {
countL++;
} else {
countU++;
}
} else if (Character.isDigit(chars[i])) {
countN++;
} else {
countO++;
}
countW = countL + countU;
}
//数字标准
if (countN > 0) {
if (countN > 1) {
sum += 20;
} else {
sum += 10;
}
}
//符号标准
if (countO > 0) {
if (countO > 1) {
sum += 25;
} else {
sum += 10;
}
}
//字母标准
if (countW > 0 && (countW == countL || countW == countU)) {
sum += 10;
} else if (countW == 0) {
sum += 0;
} else {
sum += 20;
}
//奖励
if (countN > 0 && countL > 0 && countU > 0 && countO > 0) {
sum += 5;
} else if (countW > 0 && countN > 0 && countO > 0) {
sum += 3;
} else if (countW > 0 && countN > 0 && countO == 0) {
sum += 2;
} else {
sum += 0;
}
return sum;
}
}