一、题目描述
题目链接:
二、解题思路
密码等级有五个规则判定,符合不同的条件加相应的分数,求总分,再根据总分寻找对应的密码等级。逻辑较为简单,但是在写代码时需要细心并且逻辑清晰,我认为将每种规则分别写为一个方法更为清晰。
三、代码
import java.util.Scanner;
public class Main{
public static int length(String str){//长度
if(str.length()<5){
return 5;
}else if(str.length()<8){
return 10;
}else{
return 25;
}
}
public static int letters(String str){//字母
int count1=0,count2=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>='a'&&str.charAt(i)<='z'){
count1++;
}
if(str.charAt(i)>='A'&&str.charAt(i)<='Z'){
count2++;
}
}
if(count1==0 && count2==0){
return 0;
}
if(count1!=0 && count2!=0){
return 20;
}
return 10;
}
public static int numbers(String str){//数字
int count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
count++;
}
}
if(count==0){
return 0;
}
if(count==1){
return 10;
}
return 20;
}
public static int symbols(String str){//符号
int count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>=0x21 && str.charAt(i)<=0x2F ||
str.charAt(i)>=0x3A && str.charAt(i)<=0x40 ||
str.charAt(i)>=0x5B && str.charAt(i)<=0x60 ||
str.charAt(i)>=0x7B && str.charAt(i)<=0x7E ){
count++;
}
}
if(count==0){
return 0;
}
if(count==1){
return 10;
}
return 25;
}
public static int rewards(String str){//奖励
int letters=letters(str);//字母
int numbers=numbers(str);//数字
int symbols=symbols(str);//符号
if(letters>0 && numbers>0 && symbols==0){//字母和数字
return 2;
}
if(letters==10 && numbers>0 && symbols>0){//字母、数字和符号
return 3;
}
if(letters==20 && numbers>0 && symbols>0){//大小写字母、数字和符号
return 5;
}
return 0;
}
public static String level(String str){//评分标准
int length=length(str);//长度
int letters=letters(str);//字母
int numbers=numbers(str);//数字
int symbols=symbols(str);//符号
int rewards=rewards(str);//奖励
int sum=length+letters+numbers+symbols+rewards;
if(sum>=90){
return "VERY_SECURE";//非常安全
}else if(sum>=80){
return "SECURE";//安全
}else if(sum>=70){
return "VERY_STRONG";//非常强
}else if(sum>=60){
return "STRONG";//强
}else if(sum>=50){
return "AVERAGE";//一般
}else if(sum>=25){
return "WEAK";//弱
}else{
return "VERY_WEAK";//非常弱
}
}
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
System.out.println(level(str));
}
}