【发布时间】:2021-03-13 23:05:13
【问题描述】:
我正在使用下面的脚本来检查我的密码的长度、大写字母的数量和数字的数量。
有没有办法让它也检查小写字母的数量?我试图修改我的代码几次,但每次我这样做都会踢出另外两个检查。提前致谢!
import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean success=false;
while(!success){
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
if(PassCheck(inputPassword).equals("Valid Password")) success = true;
System.out.println("");
}
}
public static String PassCheck(String Password) {
String result = "Valid Password";
int length = 0;
int numCount = 0;
int capCount = 0;
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
(Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
capCount++;
}
length = (x + 1);
}
if (numCount < 2) {
result = "digits";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (numCount < 2 && capCount < 2)
{
result = "uppercase letters digits";
}
if (length < 2) {
result = "Password is Too Short!";
}
return (result);
}
}
【问题讨论】:
-
您是否尝试使用
lowercaseCount等新变量复制检查是否增加capCount的三行? -
@VGR 我做到了。我使用了变量 lowCapCount,但是当我添加三个新行时,它会退出对 numCount 和 capCount 变量的验证。你有什么其他的建议?再次提前感谢!
-
在您的代码中没有单独检查小写字母。
-
@imraklr 我知道还没有单独检查小写字母。每次我尝试添加一个 lowCapCount 变量时,它都会退出检查大写字母的数量和字母的数量,之后只检查小写字母。我想知道我的代码是否做错了什么?如果我写问题的方式不清楚,我很抱歉,我是 Java 新手,对 Stackoverflow 也比较陌生,我只是为这个项目寻求一些专业帮助。不过感谢您的帮助。
-
@Kay 您能否在您的代码中包含对小写字母条件的检查?我没有发现任何错误。
标签: java loops validation passwords