如何让Java重复密码?

watbbzwu  于 2022-10-15  发布在  Java
关注(0)|答案(1)|浏览(147)

我接到了一个任务,我应该为单个用户编写一个简单的访问控制系统。Write a simple access control system for a single user. The user should write a non-empty username, and thereafter a password, and then he should repeat the password. The criteria for the password is her:

public static boolean checkPWD (String password){

    boolean valid = false

    int upperCount = 0;
    int lowerCount = 0;
    int digitCount = 0;
    int letterCount = 0;

        if (password.length()<8) {
            return false;   
        }

        for (int i = 0; i< password.length();i++) {
            char c = password.charAt(i);

             if(!Character.isDigit(c) && !Character.isLetter(c)&& !Character.isLowerCase(c) && !Character.isUpperCase(c)){
                return false;
            }

            if (Character.isDigit(c)) {
                digitCount++;   
            } 
            else if(Character.isLetter(c)) {
                letterCount++;
                 if (Character.isLowerCase(c)) {
                    lowerCount++;
                }
                 else if(Character.isUpperCase(c)){
                    upperCount++;
                }
                  if (c!=upperCount || c!=lowerCount || c!=letterCount || c!=digitCount){
                     valid =false;
                 }
            } 

        }   

        if (digitCount >= 2 && letterCount >= 2 && upperCount >= 1 && lowerCount >= 1) {
            valid = true;
        }

        return valid;

            }
}

`
我已经这么做了,这不是问题所在。我不知道如何让它重复密码,如果它不符合标准,它应该要求写一个新的密码。希望有人能帮忙:)下面您将看到我的代码:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //Here is for the username
    Scanner scan = new Scanner (System.in);

    boolean valid = false;
    System.out.println("Please enter a username: ");
    String Username;
    Username = scan.nextLine();

            if(Username==null) {
                valid = false;
            }

//Now they would write a password

    while(true) {
        System.out.println("please enter your password: ");
        String password;
        Scanner pscan = new Scanner (System.in);
        password = pscan.nextLine();
        if(Password.checkPWD(password)){
            System.out.println("please repeat the password");
        } else {
            System.out.println("The password is not accepted"); 
        }
        Scanner rpscan = new Scanner (System.in);
        String repeat_password;
        repeat_password = rpscan.nextLine();
        if (Password.checkPWD(password)&& pscan==rpscan) {
    System.out.println(Username+" "+"is now registered");
        }else {
        System.out.println("The repeated Password does not match");
        }
    break;}
}

}

pgpifvop

pgpifvop1#

您可以使用While循环来检查密码是否与条件匹配,如果不匹配,则提示用户输入新密码。

<code>boolean valid = false;
String password;
while (!valid) {
    System.out.println("Enter password");
    password = scan.nextLine();
    if (checkPWD(password)) {
        valid = true;
    } else {
        System.out.println("The password is not accepted");
    }
}
</code>

相关问题