【发布时间】:2018-06-07 21:36:16
【问题描述】:
我遇到的问题是当有人输入包含数字、大写和小写的密码时,程序仍然说他们需要所有这些。
如果不满足,我需要程序列出所有密码要求。
例如:
A123456789aa 返回:“您至少需要一个小写字母。请输入您的密码:”
它应该返回:“输入终止键”
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
boolean condition = true;
while (condition) {
System.out.print("Please Enter Your Password: ");
String password = input.nextLine();
if (password.equals("endofinput")) {
System.out.print("Your password is valid"); condition = false;
} else {
System.out.print(passCheck(password));
condition = true;
}
}
input.close();
}
public static String passCheck(String password) {
String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,." + " ";
if (password.length() < 8) {
return ("You need at least 8 characters. ");
} for (int i = 0; i < password.length(); i++) {
if (specialChars.contains(password.substring(i)))
return ("Your password cannot contain special characters. ");
else if (password.equals("password"))
return ("Your password cannot be password. ");
else if(!Character.isUpperCase(password.charAt(i)))
return ("You need at least one uppercase. ");
else if (!Character.isLowerCase(password.charAt(i)))
return ("You need at least one lowercase. ");
else if (!Character.isDigit(password.charAt(i)))
return ("You need at least one digit. ");
}
return "Enter termination key";
}
【问题讨论】:
-
此链接stackoverflow.com/questions/3802192/… 解释了如何使用正则表达式验证密码输入,但有很多限制
标签: java error-handling passwords