题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int sum = getLengthScore(s) + getLetterScore(s) + getNumberScore(
s) + getSignScore(s) + getRewardScore(s);
if (sum >= 90) System.out.println("VERY_SECURE");
else if (sum >= 80) System.out.println("SECURE");
else if (sum >= 70) System.out.println("VERY_STRONG");
else if (sum >= 60) System.out.println("STRONG");
else if (sum >= 50) System.out.println("AVERAGE");
else if (sum >= 25) System.out.println("WEAK");
else System.out.println("VERY_WEAK");
}
private static int getRewardScore(String s) {
if (getNumberScore(s) > 0 && getSignScore(s) > 0 &&
getLetterScore(s) == 20) return 5;
if (getNumberScore(s) > 0 && getSignScore(s) > 0 &&
getLetterScore(s) == 10) return 3;
if (getLetterScore(s) > 0 && getNumberScore(s) > 0) return 2;
return 0;
}
private static int getSignScore(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c >= 0x21 && c <= 0x2F) || (c >= 0x3A && c <= 0x40) || (c >= 0x5B &&
c <= 0x60) || (c >= 0x7B && c <= 0x7E))
count++;
}
if (count > 1) return 25;
if (count == 1) return 10;
return 0;
}
private static int getNumberScore(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c <= '9' && c >= '0') count++;
}
if (count > 1) return 20;
if (count == 1) return 10;
return 0;
}
private static int getLetterScore(String s) {
boolean haveLower = false;
boolean haveUpper = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c <= 'z' && c >= 'a') haveLower = true;
if (c <= 'Z' && c >= 'A') haveUpper = true;
}
if (haveUpper && haveLower) return 20;
if (haveUpper || haveLower) return 10;
return 0;
}
private static int getLengthScore(String s) {
if (s.length() >= 8) return 25;
if (s.length() >= 5) return 10;
return 5;
}
}
