将输入的密码字符串转换成列表,根据题目密码强度规则依次对列表进行遍历,判断来影响变量或列表标志,根据标志进行判断,加分。
源代码
import java.util.Scanner; import java.util.ArrayList; interface Judge{ public static String GetPwdSecurityLevel(String pPasswordStr) { ArrayList<Character> list=new ArrayList<Character>();//密码列表 //添加元素 for(int i=0;i<pPasswordStr.length();i++) list.add(pPasswordStr.charAt(i)); //判断长度 int score=0; if(list.size()<=4) score+=5; else if(list.size()>=5&&list.size()<=7) score+=10; else score+=25; //判断字母 ArrayList<Integer> listCha=new ArrayList<Integer>(); for(int i=0;i<list.size();i++) { if(list.get(i)>=65&&list.get(i)<=90) listCha.add(1); else if(list.get(i)>=97&&list.get(i)<=122) listCha.add(2); } if(listCha.contains(1)&&listCha.contains(2)) score+=20; else if(listCha.contains(1)) score+=10; else if(listCha.contains(2)) score+=10; //判断数字 int count=0; for(int i=0;i<list.size();i++) if(list.get(i)>=48&&list.get(i)<=57) count++; if(count==1) score+=10; else if(count>1) score+=20; //判断符号 int count2=0; for(int i=0;i<list.size();i++) if(list.get(i)>=32&&list.get(i)<=47||list.get(i)>=58&&list.get(i)<=64||list.get(i)>=91&&list.get(i)<=96||list.get(i)>=123&&list.get(i)<=126) count2++; if(count2==1) score+=10; else if(count2>1) score+=25; //奖励分 if((listCha.contains(1)&&listCha.contains(2))&&count>=1&&count2>=1) score+=5; else if((listCha.contains(1)||listCha.contains(2))&&count>=1&&count2>=1) score+=3; else if((listCha.contains(1)||listCha.contains(2))&&count>=1) score+=2; //System.out.println(score); //返回等级 if(score>=90) return "VERY_SECURE"; else if(score>=80) return "SECURE"; else if(score>=70) return "VERY_STRONG"; else if(score>=60) return "STRONG"; else if(score>=50) return "AVERAGE"; else if(score>=25) return "WEAK"; else if(score>=0) return "VERY_WEAK"; else return null; } } public class Main implements Judge{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNextLine()) System.out.println(Judge.GetPwdSecurityLevel(sc.nextLine())); sc.close(); } }