对于我的密码强度检查器,我无法显示从输入的密码中获得的分数。下面是代码,问题一直显示在我的代码底部:
import java.lang.Thread.State;
import java.util.Scanner;
public class PasswordUtils {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Password to test: ");
String pw = sc.nextLine();
if (containsUpperCase(pw)) {
System.out.println(" ...contains an upper case letter");
}
if (containsLowerCase(pw)) {
System.out.println(" ...contains an lower case letter");
}
if (containsDigit(pw)) {
System.out.println(" ...contains a number");
}
if (containsSpecial(pw)) {
System.out.println(" ...contains a number");
}
sc.close();
}
/ ** *确定给定的字符串是否包含大写字母 * @param是要检查的字符串 * @return当且仅当s包含大写字母时才返回true * / public static boolean containsUpperCase(String s){ for(int letter = 0; letter = 0){ 返回true; } } 返回false; }
public static boolean containsLowerCase(String s) {
for (int letter=0; letter<s.length(); letter++) {
if ("abcdefghijklmnopqrstuvwxyz".indexOf(s.charAt(letter))>=0) {
return true;
}
}
return false;
}
public static boolean containsDigit(String s) {
for (int letter=0; letter<s.length(); letter++) {
if ("1234567890".indexOf(s.charAt(letter))>=0) {
return true;
}
}
return false;
}
public static boolean containsSpecial(String s) {
for (int letter=0; letter<s.length(); letter++) {
if ("!@#$%^&*()_-+=[]{};:,.<>?/|".indexOf(s.charAt(letter))>=0) {
return true;
}
}
return false;
}
/**
* Determine the actual strength of a password based upon various tests
* @param s the password to evaluate
* @return the strength (on a 1 to 5 scale, 5 is very good) of the password
*/
public static int score(String s) {
int pwscore = 0;
//if it contains one digit, add 1 to total score
if( s.matches("(?=.*[0-9]).*") )
pwscore += 1;
//if it contains one lower case letter, add 1 to total score
if( s.matches("(?=.*[a-z]).*") )
pwscore += 1;
//if it contains one upper case letter, add 1 to total score
if( s.matches("(?=.*[A-Z]).*") )
pwscore += 1;
//if it contains one special character, add 1 to total score
if( s.matches("(?=.*[~!@#$%^&*()_-]).*") )
pwscore += 1;
System.out.println("The password strength is " + pwscore);
return pwscore; // Right now, all passwords stink!!!
}
}
答案 0 :(得分:0)
而不是重新实施测试,并且你说你已经有了功能代码 - 我更喜欢这样,比如 -
public static int score(String pw) {
int score = 1; // <-- 1 to 5...
if (containsUpperCase(pw)) {
score++;
}
if (containsLowerCase(pw)) {
score++;
}
if (containsDigit(pw)) {
score++;
}
if (containsSpecial(pw)) {
score++;
}
return score;
}
然后调用它并将结果打印在main
中
System.out.println("The score is " + score(pw));