【问题标题】:Check password, verify, and loop again检查密码,验证,然后再次循环
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您的帮助。这解决了问题!
    【解决方案2】:

    您可以通过将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;
        }
    }
    

    【讨论】:

    • 我正要说这个,但他想读出问题所在,所以布尔值不够好。它有多种错误的方式。
    • @Smartsav10 是的,并且对于每个不符合规则的条件几乎都是错误的。
    • 这正是我要说的。用户希望能够区分密码无效的不同方式。您需要使用某种字符串或整数。
    • @Smartsav10 根据代码区分显示在result 并打印
    【解决方案3】:

    以下是在 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);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2021-03-27
      • 2020-08-17
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 2015-08-18
      相关资源
      最近更新 更多