首页 > 解决方案 > 如何让我的代码继续接收用户输入,直到密码与参数匹配?

问题描述

我试图让我的代码循环,直到用户输入符合密码参数“请输入具有以下条件的密码:密码必须包含至少 8 个字符、1 个大写字母、1 个小写字母、1 个数字

Password must not contain the word 'password' , spaces (' ') or  special characters");

我正在努力让我的 do-while 循环工作。任何帮助将非常感激。

代码片段:

import java.util.Scanner; 

public class PasswordValidation
{
public static void main(String[]args){

  boolean valid = false;
  System.out.println("Please enter password with the following criteria");
  System.out.println("Password must contain atleast 8 characters, 1 uppercase letter, 1 lowercase letter,1 number");
  System.out.println("Password must not contain the word 'password' , spaces (' ') or  special characters");

  do {
   Scanner input = new Scanner(System.in);
   String password = input.nextLine();




            if (password.length() >= 8)
            {
                    System.out.println("Password must be atleast 8 characters in length.");
                    valid = false;
            }

            String upperCaseChars = "(.*[A-Z].*)";
            if (!password.matches(upperCaseChars ))
            {
                    System.out.println("Password should contain atleast one upper case letter");
                    valid = false;
            }
            String lowerCaseChars = "(.*[a-z].*)";
            if (!password.matches(lowerCaseChars ))
            {
                    System.out.println("Password should contain atleast one lower case letter");
                    valid = false;
            }
            String num = "(.*[0-9].*)";
            if (!password.matches(num ))
            {
                    System.out.println("Password should contain atleast one number.");
                    valid = false;
            }
            String specialChar = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
            if (password.matches(specialChar ))
            {
                    System.out.println("Password should not contain special character");
                    valid = false;
            }
            if (password ==  "password")
            {
                    System.out.println("Password Should not be 'password'");
                    valid = false;
            }
            else
            {
                    valid = true; 
                    System.out.println("Password is valid.");
            }
        }
    } while valid = false; 

}

标签: java

解决方案


如果您在 do-while 循环之前存储所有 String 值,则需要更少的计算时间。因为它不需要每次都初始化字符串变量。而且,如果你使用 else-if 阶梯,那么它也会减少执行时间。它应该是这样的:

        Scanner input = new Scanner(System.in);
        boolean valid;
        String upperCaseChars = "(.*[A-Z].*)";
        String lowerCaseChars = "(.*[a-z].*)";
        String num = "(.*[0-9].*)";
        String specialChar = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
        System.out.println("Please enter password with the following criteria");
        System.out.println("Password must contain atleast 8 characters, 1 uppercase letter, 1 lowercase letter,1 number");
        System.out.println("Password must not contain the word 'password' , spaces (' ') or  special characters");

        do {
            String password = input.nextLine();

            if (password.length() < 8) {
                System.out.println("Password must be atleast 8 characters in length.");
                valid = false;
            }

            else if (!password.matches(upperCaseChars)) {
                System.out.println("Password should contain atleast one upper case letter");
                valid = false;
            }

            else if (!password.matches(lowerCaseChars)) {
                System.out.println("Password should contain atleast one lower case letter");
                valid = false;
            }

            else if (!password.matches(num)) {
                System.out.println("Password should contain atleast one number.");
                valid = false;
            }

            else if (password.matches(specialChar)) {
                System.out.println("Password should not contain special character");
                valid = false;
            }
            else if (password == "password") {
                System.out.println("Password Should not be 'password'");
                valid = false;
            } else {
                valid = true;
                System.out.println("Password is valid.");
            }
        }
        while (valid == false);

推荐阅读