【发布时间】:2021-03-12 23:18:45
【问题描述】:
我有一个需要至少 2 个大写字母、至少 3 个小写字母和 1 个数字的问题。
这是确切的问题:
编写一个应用程序,提示用户输入包含至少两个大写字母、至少三个小写字母和至少一个数字的密码。不断提示用户,直到输入有效密码。如果密码有效,则显示有效密码;如果不是,请显示密码无效的相应原因如下:
例如,如果用户输入“密码”,您的程序应输出:您的密码无效,原因如下:大写字母数字
如果用户输入“passWOrd12”,你的程序应该输出:valid password
到目前为止,这是我的编码,我遇到的问题是程序提示用户输入更多密码,一旦输入错误。
import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
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 (numCount < 2 && capCount < 2) {
result = "uppercase letters digits";
}
if (length < 2) {
result = "Password is Too Short!";
}
return (result);
}
}
【问题讨论】:
标签: java loops validation passwords