在do while循环中正确循环的问题 - Java

时间:2016-03-24 23:30:54

标签: java loops do-while

我的程序似乎遇到了一些问题。我被建议我的讲师使用do while循环来保持程序运行。但是,当我输入无效密码并重新启动时,它会出错。当我输入有效密码时,它只打印“有效密码”并且循环。我哪里出错?

import java.util.Scanner;

public class CovenduniteProg5_FIX {

    public static void main(String[] args)      {
        boolean passwordValid = false;

        do  {
            boolean invalidLength = false;
            boolean containsRestrictedWord = false;
            boolean containsLowerCaseLetter = false;
            boolean containsUpperCaseLetter = false;
            boolean containsDigit = false;
            boolean containsSpecialChar = false;

            Scanner stdIn = new Scanner(System.in);

            System.out.println("Password Verifier"); 
            System.out.println("\nEnter a password that meets the following rules:\n" +
                "\tIs at least 8 characters long\n" + 
                "\tContains at least 1 lower letter character\n" +
                "\tContains at least 1 upper letter character\n" +
                "\tContains at least 1 numberic digit\n" +
                "\tContains at least 1 special character from the set: !@#$%^&*\n" +
                "\tDoes not contain the word \"and\" or the word \"the\"\n");
            System.out.print("Enter your password: ");
            String password = stdIn.nextLine();

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

                if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
                    containsUpperCaseLetter = true;
                }

                if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
                    containsLowerCaseLetter = true;
                }

                if (Character.isDigit(ch) && !containsDigit) {
                    containsDigit = true;
                }

                if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&' || ch == '*') && !containsSpecialChar) {
                    containsSpecialChar = true;
                }
            }

            if (password.length() < 8 ) {
                invalidLength = true;
            } 

            if (password.contains("and") || password.contains("the")) {
                containsRestrictedWord = true;
            }

            if (invalidLength) {
                System.out.println("Invalid: Invalid length");
            }

            if (containsRestrictedWord) {
                System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
            }

            if (!containsDigit) {
                System.out.println("Invalid: Does not contain at least one digit.");
            }

            if (!containsLowerCaseLetter) {
                System.out.println("Invalid: Does not contain at least one lowercase letter.");
            }

            if (!containsUpperCaseLetter) {
                System.out.println("Invalid: Does not contain at least one uppercase letter.");
            }

            if(!containsSpecialChar) {
                System.out.println("Invalid: Does not contain at least one special character.");
            }

            if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit && !containsRestrictedWord && !invalidLength)
                passwordValid = true;

            if (passwordValid)
                System.out.print("\nPassword is valid.");

        } while (!passwordValid);

        passwordValid = false;

        while(!passwordValid) {
            if (passwordValid)
                System.out.print("\nPassword is valid.");
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您将最后一个passwordValid = false;放在do-while之外,当您在循环之前只需要它一次。而且您也不需要第二个while(!passwordValid)循环:

boolean passwordValid = false; //← only here
do  {
    boolean invalidLength = false;
    boolean containsRestrictedWord = false;
    boolean containsLowerCaseLetter = false;
    boolean containsUpperCaseLetter = false;
    boolean containsDigit = false;
    boolean containsSpecialChar = false;

    Scanner stdIn = new Scanner(System.in);

    /*

    ...

    */

    if (/*All sub-conditions met*/)
        passwordValid = true;

    if (passwordValid)
        System.out.print("\nPassword is valid.");
}
while (!passwordValid);
//passwordValid = false; ← remove this

//no need for while or if statment, because you can only reach this code if passwordValid = true
System.out.print("\nPassword is valid.");

答案 1 :(得分:0)

我们不需要第二个while循环,它基本上是infinite loop。此外,我们需要在while循环之外移动“密码验证程序”语句,因为它们不需要多次打印。所以,该方法看起来像这样:

public static void main(String[] args) {

    boolean passwordValid = false;
    System.out.println("Password Verifier");
    System.out.println("\nEnter a password that meets the following rules:\n" + "\tIs at least 8 characters long\n"
            + "\tContains at least 1 lower letter character\n" + "\tContains at least 1 upper letter character\n"
            + "\tContains at least 1 numberic digit\n"
            + "\tContains at least 1 special character from the set: !@#$%^&*\n"
            + "\tDoes not contain the word \"and\" or the word \"the\"\n");
    do {
        boolean invalidLength = false;
        boolean containsRestrictedWord = false;
        boolean containsLowerCaseLetter = false;
        boolean containsUpperCaseLetter = false;
        boolean containsDigit = false;
        boolean containsSpecialChar = false;

        Scanner stdIn = new Scanner(System.in);

        System.out.print("Enter your password: ");
        String password = stdIn.nextLine();

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

            if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
                containsUpperCaseLetter = true;
            }

            if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
                containsLowerCaseLetter = true;
            }

            if (Character.isDigit(ch) && !containsDigit) {
                containsDigit = true;
            }

            if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&'
                    || ch == '*') && !containsSpecialChar) {
                containsSpecialChar = true;
            }
        }

        if (password.length() < 8) {
            invalidLength = true;
        }

        if (password.contains("and") || password.contains("the")) {
            containsRestrictedWord = true;
        }

        if (invalidLength) {
            System.out.println("Invalid: Invalid length");
        }

        if (containsRestrictedWord) {
            System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
        }

        if (!containsDigit) {
            System.out.println("Invalid: Does not contain at least one digit.");
        }

        if (!containsLowerCaseLetter) {
            System.out.println("Invalid: Does not contain at least one lowercase letter.");
        }

        if (!containsUpperCaseLetter) {
            System.out.println("Invalid: Does not contain at least one uppercase letter.");
        }

        if (!containsSpecialChar) {
            System.out.println("Invalid: Does not contain at least one special character.");
        }

        if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit
                && !containsRestrictedWord && !invalidLength)
            passwordValid = true;

        if (passwordValid)
            System.out.print("\nPassword is valid.");

    } while (!passwordValid);
}

答案 2 :(得分:0)

以下代码将按您的要求执行。有些事情需要考虑:

  • 第二个while循环是无限的。
  • 在do循环之外声明扫描程序...每次创建一个可能会导致问题,您只需要声明并创建一次。
  • 使用两个do循环,外循环具有无限条件,因此它将永远持续...您可以重写以某种方式终止。
  • 外部循环显示您的菜单。
  • 内部循环完成所有扫描/输入,验证和确定。

希望这有帮助

// CovenduniteProg5_FIX
public static void main(String[] args) {
    boolean passwordValid = false;
    Scanner stdIn = new Scanner(System.in);

    do {
        System.out.println("Password Verifier");
        System.out.println("\nEnter a password that meets the following rules:\n"
                + "\tIs at least 8 characters long\n" + "\tContains at least 1 lower letter character\n"
                + "\tContains at least 1 upper letter character\n" + "\tContains at least 1 numberic digit\n"
                + "\tContains at least 1 special character from the set: !@#$%^&*\n"
                + "\tDoes not contain the word \"and\" or the word \"the\"\n");
        System.out.print("Enter your password: ");

        do {
            boolean invalidLength = false;
            boolean containsRestrictedWord = false;
            boolean containsLowerCaseLetter = false;
            boolean containsUpperCaseLetter = false;
            boolean containsDigit = false;
            boolean containsSpecialChar = false;

            try { 
                String password = stdIn.nextLine();

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

                    if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
                        containsUpperCaseLetter = true;
                    }

                    if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
                        containsLowerCaseLetter = true;
                    }

                    if (Character.isDigit(ch) && !containsDigit) {
                        containsDigit = true;
                    }

                    if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&'
                            || ch == '*') && !containsSpecialChar) {
                        containsSpecialChar = true;
                    }
                }

                if (password.length() < 8) {
                    invalidLength = true;
                }

                if (password.contains("and") || password.contains("the")) {
                    containsRestrictedWord = true;
                }

                if (invalidLength) {
                    System.out.println("Invalid: Invalid length");
                }

                if (containsRestrictedWord) {
                    System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
                }

                if (!containsDigit) {
                    System.out.println("Invalid: Does not contain at least one digit.");
                }

                if (!containsLowerCaseLetter) {
                    System.out.println("Invalid: Does not contain at least one lowercase letter.");
                }

                if (!containsUpperCaseLetter) {
                    System.out.println("Invalid: Does not contain at least one uppercase letter.");
                }

                if (!containsSpecialChar) {
                    System.out.println("Invalid: Does not contain at least one special character.");
                }

                if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit
                        && !containsRestrictedWord && !invalidLength)
                    passwordValid = true;

                if (passwordValid)
                    System.out.print("\nPassword is valid.");

            }
            catch ( Exception ltheXcp ) { 
                ltheXcp.printStackTrace();
            }
        } while (!passwordValid);

        passwordValid = false;

    } while ( 1 != 2 );

//        while (!passwordValid) {
//            if (passwordValid)
//                System.out.print("\nPassword is valid.");
//        }
}