简体   繁体   English

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

[英]Issues with proper looping in a do while loop - Java

I seem to be having some issues with my program. 我的程序似乎遇到了一些问题。 I was advised to use a do while loop by my instructor to keep the program running. 我被建议我的讲师使用do while循环来保持程序运行。 However, it errors out when I type in an invalid password and restarts. 但是,当我输入无效密码并重新启动时,它会出错。 When I input a valid password it only prints "Valid Password" and does not loop. 当我输入有效密码时,它只打印“有效密码” 而不循环。 Where have I gone wrong? 我哪里出错了?

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

You put the last passwordValid = false; 你把最后一个passwordValid = false; outside the do-while when you should just have it once before the loop. do-while之外你应该在循环之前拥有它一次。 And you also do not need the second while(!passwordValid) loop: 而且你也不需要第二个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.");

We don't need the second while loop, it's basically an infinite loop . 我们不需要第二个while循环,它基本上是一个infinite loop Also, we need to move the "Password Verifier" statements outside the while loop as they needn't be printed multiple times. 另外,由于不需要多次打印,因此我们需要将“ Password Verifier”语句移至while循环之外。 So, the method will look something like this: 因此,该方法将如下所示:

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

The following code will do what you ask. 以下代码将按您的要求执行。 Some things to consider : 有些事情需要考虑:

  • The second while loop is infinite. 第二个while循环是无限的。
  • Declare the scanner outside the do loops...creating one everytime could cause issues and you only need to declare and create it once. 在do循环之外声明扫描程序...每次创建一个都可能导致问题,您只需要声明并创建一次。
  • Use two do loops, the outer loop has an infinite condition so it will continue forever...you can re-write to terminate in some fashion. 使用两个do循环,外循环具有无限条件,因此它将永远持续...您可以重写以某种方式终止。
  • the outer loop displays your menu. 外循环显示菜单。
  • the inner loop does all you scanner/input and validation and determination. 内循环完成所有扫描器/输入以及验证和确定。

Hope this helps 希望这可以帮助

// 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.");
//        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM