所以我写了一个让用户输入密码的方法,这个密码必须通过以下规范:
1. 长度至少为 8 位数字
2. 大写
3. 小写
4. 有特殊数字
我不确定为什么当我输入它时,输出不考虑特殊字符并引发错误。
到目前为止,这是我的代码:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter a given password : ");
String passwordhere = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String confirmhere = in.nextLine();
System.out.println("your password is: " + passwordhere);
while (!passwordhere.equals(confirmhere) || !isValid(passwordhere)) {
System.out.println("The password entered here is invalid");
System.out.print("Please enter the password again.it must be valid : ");
String Passwordhere = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
}
}
public static boolean isValid(String passwordhere) {
if (passwordhere.length() < 8) {
return false;
} else {
for (int p = 0; p < passwordhere.length(); p++) {
if (Character.isUpperCase(passwordhere.charAt(p))) {
}
}
for (int q = 0; q < passwordhere.length(); q++) {
if (Character.isLowerCase(passwordhere.charAt(q))) {
}
}
for (int r = 0; r < passwordhere.length(); r++) {
if (Character.isDigit(passwordhere.charAt(r))) {
}
}
for (int s = 0; s < passwordhere.length(); s++) {
if (Character.isSpecialCharacter(passwordhere.charAt(s))) {
}
}
return true;
}
}
另外,另一个问题是,假设用户输入 bob123
作为他们的密码。
我怎样才能让循环告诉用户它需要什么是正确的密码?
在上面的示例中,它缺少一个大写字母和一个符号(*&^..等)。
每次用户输入密码时,我如何添加它以打印出来,直到他们获得正确的密码以通过代码的所有规范?
原文由 Progamminnoob 发布,翻译遵循 CC BY-SA 4.0 许可协议
你应该清楚地提到你的要求我不知道你的要求。请找到我下面的解决方案`