题解 | #密码强度等级#
密码强度等级
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); while (sc.hasNext()) { String str = sc.nextLine(); int score = 0; int length = str.length(); // 密码长度得分 if(length <= 4){ score += 5; }else if(length <= 7 && length >= 5){ score += 10; }else if(length >= 8){ score += 25; } int lowerLetterCount = 0; int upperLetterCount = 0; int numberCount = 0; int symbolCount = 0; char[] chars = str.toCharArray(); for(char c : chars){ if(c >= 'a' && c <= 'z'){ lowerLetterCount++; }else if(c >= 'A' && c <= 'Z'){ upperLetterCount++; }else if(c >= '0' && c <= '9'){ numberCount++; }else{ symbolCount++; } } // 字母得分 if(lowerLetterCount > 0 && upperLetterCount > 0){ score += 20; }else if(lowerLetterCount == 0 || upperLetterCount == 0){ score += 10; } // 数字得分 if(numberCount > 1){ score += 20; }else if(numberCount == 1){ score += 10; } // 符号得分 if(symbolCount > 1){ score += 25; }else if(symbolCount == 1){ score += 10; } // 奖励 if(lowerLetterCount > 0 && upperLetterCount > 0 && numberCount > 0 && symbolCount > 0){ score += 5; }else if((lowerLetterCount > 0 || upperLetterCount > 0) && numberCount > 0 && symbolCount > 0){ score += 3; }else if((lowerLetterCount > 0 || upperLetterCount > 0) && numberCount > 0){ 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 if(score >= 0){ System.out.println("VERY_WEAK"); } } } }