题解 | #密码强度等级#
密码强度等级
http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.Scanner; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); Main sol = new Main(); String rs = sol.pwdStrength(s); System.out.println(rs); } public String pwdStrength (String s) { // 分数 int fraction = 0; // 密码长度 int s_length = s.length(); // 含有字母的个数 int letter_count = s.replaceAll("[^a-zA-Z]", "").length(); // 含有的数字个数 int number_count = s.replaceAll("[^0-9]", "").length(); // 含有的符号个数 int char_count = s.replaceAll("[0-9a-zA-Z]", "").length(); // 密码长度得分 if (s_length <= 4) { fraction += 5; } else if (s_length <= 7) { fraction += 10; } else if (s_length >= 8) { fraction += 25; } // 是否含大小写字母 boolean upper_letter = Pattern.matches(".*[A-Z]+.*", s); boolean lower_letter = s.matches(".*[a-z]+.*"); // 仅大/小写字母的个数 int upper_letter_count = s.replaceAll("[^A-Z]", "").length(); int lower_letter_count = s.replaceAll("[^a-z]", "").length(); // 含字母-得分 if (lower_letter && upper_letter) { fraction += 20; } else if (upper_letter_count > 0) { fraction += 10; } else if (lower_letter_count > 0) { fraction += 10; } else if (letter_count == 0) { fraction += 0; } // 数字-得分 if (number_count == 0) { fraction += 0; } else if (number_count == 1) { fraction += 10; } else if (number_count > 1) { fraction += 20; } // 符号-得分 if (char_count == 0) { fraction += 0; } else if (char_count == 1) { fraction += 10; } else if (char_count > 1) { fraction += 25; } // 奖励-得分 if (upper_letter && lower_letter && number_count > 0 && char_count > 0) { fraction += 5; } else if (letter_count > 0 && number_count > 0 && char_count > 0) { fraction += 3; } else if (letter_count > 0 && number_count > 0) { fraction += 2; } if (fraction >= 90) { return "VERY_SECURE"; } else if (fraction >= 80) { return "SECURE"; } else if (fraction >= 70) { return "VERY_STRONG"; } else if (fraction >= 60) { return "STRONG"; } else if (fraction >= 50) { return "AVERAGE"; } else if (fraction >= 25) { return "WEAK"; } else if (fraction >= 0) { return "VERY_WEAK"; } return null; } }