题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String pwd = sc.nextLine(); int score = 0; // 密码得分 score = lengthScore(pwd) + numScore(pwd) + alphabetScore(pwd) + signScore(pwd) + award(pwd); // 得分等级 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 { System.out.println("VERY_WEAK"); } } // 长度分 public static int lengthScore(String pwd){ if(pwd.length() <= 4){ return 5; } else if(pwd.length() <= 7){ return 10; } else { return 25; } } // 字母分 public static int alphabetScore(String pwd){ int[] level = new int[2]; for (char c : pwd.toCharArray()) { if(c >= 65 && c <= 90){ level[0] = 1; } else if(c >= 97 && c <= 122){ level[1] = 1; } } if(level[0] + level[1] == 0){ return 0; } else if(level[0] + level[1] == 1){ return 10; } else { return 20; } } // 数字分 public static int numScore(String pwd){ int count = 0; for (char c : pwd.toCharArray()) { if(c >= 48 && c <= 57){ count++; } } if(count == 0){ return 0; } else if(count == 1){ return 10; } else { return 20; } } // 符号分 public static int signScore(String pwd){ int count = 0; for (char c : pwd.toCharArray()) { if("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".indexOf(c) != -1){ count++; } } if(count == 0){ return 0; } else if(count == 1){ return 10; } else { return 25; } } // 奖励分 public static int award(String pwd){ if(alphabetScore(pwd) == 20 && numScore(pwd) != 0 && signScore(pwd) != 0){ return 5; } else if(alphabetScore(pwd) == 10 && numScore(pwd) != 0 && signScore(pwd) != 0){ return 3; } else if(alphabetScore(pwd) != 0 && numScore(pwd) != 0 && signScore(pwd) == 0){ return 2; } else { return 0; } } }