题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String pwd = in.nextLine();
int score = 0;
//奖励标志位,分别表示大写字母,小写字母,数字,符号
int[] reward = {0, 0, 0, 0};
// 长度
if (pwd.length() >= 8){
score += 25;
}else if (pwd.length() >= 5){
score += 10;
}else{
score += 5;
}
// 字母
Matcher matcher = Pattern.compile("(?=.*[a-z])(?=.*[A-Z])").matcher(pwd);
Matcher matcher1 = Pattern.compile("[a-z]+|[A-Z]+").matcher(pwd);
if (matcher.find()){ //字母大小混合
score += 20;
reward[0] = reward[1] = 1;
}else if (matcher1.find()){ //只有大写字母或者小写字母
score += 10;
reward[0] = 1; //只有大写或者小写字母时计大写字母标志位为1
} //没有字母不加分
//数字
Matcher matcher2 = Pattern.compile("\\d").matcher(pwd);
int count = 0;
while (matcher2.find()){
count++;
}
if (count == 1){ // 只有1个数字
score += 10;
reward[2] = 1;
}else if(count > 1){ //多个数字
score += 20;
reward[2] = 1;
}
//符号
String regex = "[!\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~]";
Matcher matcher3 = Pattern.compile(regex).matcher(pwd);
count = 0;
while (matcher3.find()){
count++;
}
if (count == 1){ // 只有1个符号
score += 10;
reward[3] = 1;
}else if(count > 1){ //多个符号
score += 25;
reward[3] = 1;
}
//奖励
if (reward[0] == 1 && reward[1] == 1 && reward[2] == 1 && reward[3] == 1){
score += 5;
}else if (reward[0] == 1 && reward[2] == 1 && reward[3] == 1){
score += 3;
}else if (reward[0] == 1 && reward[2] == 1){
score += 2;
}
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");
}
}
}
