题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while(in.hasNextLine()){
String s = in.nextLine();
int score = 0;
int len = s.length();
if(len <= 4) score+=5;
else if(len>=5 && len<=7) score+=10;
else score+=25;
boolean hasUpper = false;
boolean hasLower = false;
boolean hasDigit = false;
int countDigit = 0;
boolean hasOther = false;
int countOthers = 0;
for(int i=0 ; i<len ; ++i){
char c = s.charAt(i);
if(Character.isLetter(c)){
if(c>='a' && c<='z'){
hasLower = true;
}
if(c>='A' && c<='Z'){
hasUpper = true;
}
}
else if(Character.isDigit(c)){
hasDigit = true;
countDigit++;
}
else{
hasOther = true;
countOthers++;
}
}
if(hasLower || hasUpper){
if(hasLower&&hasUpper){
score+=20;
//System.out.println(0);
}
else{
score+=10;
}
}
if(hasDigit){
if(countDigit==1){
score+=10;
}
else{
score+=20;
}
}
if(hasOther){
if(countOthers==1){
score+=10;
}
else{
score+=25;
}
}
int sc1 = score, sc2=score, sc3=score;
if(hasUpper||hasLower&&hasDigit){
sc1+=2;
}
if(hasUpper||hasLower&&hasDigit&&hasOther){
sc2+=3;
}
if(hasUpper&&hasLower&&hasDigit&&hasOther){
sc3+=5;
}
score = Math.max(sc1, Math.max(sc2,sc3));
//System.out.println(score);
if(score>=90){
System.out.println("VERY_SECURE");
}
else if(score<90 && score>=80){
System.out.println("SECURE");
}
else if(score>=70 && score<80){
System.out.println("VERY_STRONG");
}
else if(score>=60 && score<70){
System.out.println("STRONG");
}
else if(score>=50 && score<60){
System.out.println("AVERAGE");
}
else if(score>=25 && score<50){
System.out.println("WEAK");
}
else{
System.out.println("VERY_WEAK");
}
//System.out.println(score);
}
}
}
查看28道真题和解析