检查密码、验证并再次循环

fsi0uk1n  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(310)

我有一个问题,需要至少2个大写字母,至少3个小写字母和1位数字。
下面是确切的问题:
编写一个应用程序,提示用户输入至少包含两个大写字母、至少三个小写字母和至少一个数字的密码。持续提示用户直到输入有效密码。如果密码有效,则显示有效密码;如果不是,请显示密码无效的适当原因,如下所示:
例如,如果用户输入“password”,您的程序应该输出:您的密码无效,原因如下:大写字母数字
如果用户输入“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);
    }
}
y53ybaqx

y53ybaqx1#

你需要一段时间。这将确保您的代码将保持循环,直到它成功。当它成功时,布尔值变为真并且不循环。在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("");

    } 
  }
avkwfej4

avkwfej42#

您可以通过更改 PassCheck 方法生成布尔结果并使用 Character 类来检查大小写数字字符,并在结束时计数每个字符的出现情况检查条件,如果通过,则结果为 true 不然就是了 false 你可以迭代你的 do-while 根据试验结果 PassCheck 方法

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

ubby3x7f3#

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

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

相关问题