检查密码,验证,然后再次循环



我遇到一个问题,需要至少2个大写字母、至少3个小写字母和1个数字。

这就是确切的问题:

编写一个应用程序,提示用户输入至少包含两个大写字母、至少三个小写字母和至少一个数字的密码。持续提示用户,直到输入有效密码。如果密码有效,则显示有效密码;如果没有,显示密码无效的适当原因如下:

例如,如果用户输入";密码";您的程序应该输出:您的密码无效,原因如下:大写字母数字

如果用户输入"0";passWOrd12";,您的程序应该输出:有效密码

这是我到目前为止的编码,我遇到了程序提示用户输入更多密码的问题,一旦他们输入了错误的密码。

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);
}
}

您需要一个while循环。这将确保您的代码在成功之前保持循环。当它成功时,布尔值变为true,并且不会循环。写下while循环之后你想发生的任何事情。

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("");
} 
}

您可以通过将PassCheck方法的返回类型更改为布尔结果来完成此操作,并使用Character类检查大写-小写数字字符,并在检查条件时计算每个字符的出现次数,如果通过,则结果为true,否则为false,您可以根据PassCheck方法的结果迭代器do-while

public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean isValidPassword = false;

do {
System.out.print("Password: ");
inputPassword = input.next();
isValidPassword = PassCheck(inputPassword);
System.out.println("");
}while(!isValidPassword);
}

public static boolean PassCheck(String Password) {
boolean isValid = false;
String result = "";
int numCount = 0;
int capCount = 0;
int lowCount = 0;

for (int x = 0; x < Password.length(); x++) {

if(Character.isDigit(Password.charAt(x))) {
numCount++;
}
if(Character.isUpperCase(Password.charAt(x))) {
capCount++;
}

if(Character.isLowerCase(Password.charAt(x))) {
lowCount++;
}
}

if(numCount >= 1 && capCount >= 2 && lowCount >= 3) {
result = "Password Valid";
isValid = true;
} else {
isValid = false;
result = "Password is Invalid: ";

if(Password.length() < 2) {
result += " Password is Too Short!";
}

if(numCount < 1) {
result += " no digit";
}

if(capCount < 2) {
result += " at lease 2 Uppercase";
}

if(lowCount < 3) {
result += " at lease 3 Lowercase";
}
}
System.out.println(result);
return isValid;
}
}

下面是我在圣智平台上完成的代码:

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);
}
}

最新更新