0
對於我的密碼強度檢查器,我無法顯示我從輸入的密碼中得到的分數。這是下面的代碼,這個問題是在我的代碼底部顯示一路:無法顯示密碼的強度
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();
}
/** * 確定是否給定的字符串包含大寫字母 *參數S中的字符串檢查 * @返回真當且僅當s包含大寫字母 */ 公共靜態布爾containsUpperCase(一個String){ 對(INT信= 0;字母= 0){ 返回真; } } return 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 ("[email protected]#$%^&*()_-+=[]{};:,.<>?/|".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("(?=.*[[email protected]#$%^&*()_-]).*"))
pwscore += 1;
System.out.println("The password strength is " + pwscore);
return pwscore; // Right now, all passwords stink!!!
}
}
由於打印結果,我怎麼顯示從這裏密碼強度? – V4k4
再次感謝男士。如何編寫longEnough()方法來簡單測試字符串是否包含8個或更多字母,以及如何將它納入score()方法? – V4k4