“while”循环问题为Java

时间:2013-09-19 03:50:36

标签: java while-loop

我要做的是从用户那里获取密码,然后检查4个条件。如果是的话。然后打印出“传递单词是正确的”。如果不是,请向用户显示错误并返回菜单。问题是我不能回到菜单。如果我在行//User interface //suppose to put a while loop here, but..... System.out.println(); System.out.println("Please create a password and a legal one"中执行while循环。它确实会返回,但某些'if'语句不会运行。谁能看到我被困在哪里?谢谢。

import java.util.Scanner;

public class Text_processing
{

    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);        //user input        
        String user_input;      // To hold input
        char[] array;           //Array for user_input
        int letters = 0;        // Number of letters
        int digits = 0;         // Number of digits
        boolean nu = true;      //
        boolean nl = true;
        boolean nd = true;
        boolean n7 = true;


        //User interface
        //suppose to put a while loop here, but.....
        System.out.println();
        System.out.println("Please create a password and a legal one" 
                           + " should contain the following elements:");
        System.out.println("\tAt least 7 characters in length.");
        System.out.println("\tAt least 1 upper case letter.");
        System.out.println("\tAt least 1 lower case letter.");
        System.out.println("\tAt least 1 number.");

        //Get a string from the user
        user_input = kb.nextLine();

        //Convert it to a char array
        array = user_input.toCharArray();

        for(int i = 0; i < array.length; i++)
        {
            if(!nu && !nl && !nd && !n7)
            {               
                break;
            }

            if (array.length >= 7)       //not < 7 cos breaks
            {         
                n7 = false;        
            }

            if(Character.isUpperCase(array[i]))
            {
                nu = false;             
            }

            if (Character.isLowerCase(array[i])) 
            {
                nl = false;
            }

            if (Character.isDigit(array[i])) 
            {
                nd = false;       
            }

        }

        if(n7)
        {
            System.out.println("Password does not contain 7 or more letters.");
        }
        if(nu)
        {
            System.out.println("Password does not contain an upper case letter.");
        }
        if(nl)
        {
            System.out.println("Password does not contain an lower case letter.");
        }
        if(nd)
        {
            System.out.println("Password does not contain a number.");
        }

            System.out.println("Password is correct.");


    }
}

7 个答案:

答案 0 :(得分:2)

在当前的代码中,您需要确保在主循环的每次迭代中重置nunlndn7

boolean badPassword = false;
do {
    boolean nu = true;
    boolean nl = true;
    boolean nd = true;
    boolean n7 = true;
    /*
     * Add everything from your code that appears below `boolean n7 = true;`
     * and above System.out.println("Password is correct."); here...
     */

    badPassword = (n7 || nu || nl || nd);
} while (badPassword);
System.out.println("Password is correct.");

答案 1 :(得分:0)

启用while循环有什么问题:

String user_input;      // To hold input

并在之前结束:

        System.out.println("Password is correct.");

答案 2 :(得分:0)

为什么你应该找到已经显示内容的具体原因。密码应该如何。

只需打印&#34;密码不符合上述条件请再试一次&#34;

我认为够了

答案 3 :(得分:0)

这种情况的标准模式是:

Scanner sc = new Scanner(System.in);
String userInput = null;
while (true) {
    System.out.println("please enter something:");
    userInput = sc.next();
    if (<input fails some test>) {
        System.out.println("some reason");
        continue; // try again
    }
    // repeat other tests similarly
    break; // will only get to here if input was OK
}

在输入有效输入之前,循环无法退出,之后userInput将保留该值。

另请注意如何通过将提示放在循环中,可以避免无效输入上的代码重复。

答案 4 :(得分:0)

编写一个简单的方法来验证这样的密码。

private static boolean isValid(char[] array) {
    if(array.length<7){
        return false;
    }
    else{
        boolean isUpper=false, isLower=false, isDigit=false;
        for (int i = 0; i < array.length; i++) {
            char c = array[i];
            if(Character.isUpperCase(c)){
                isUpper=true;
            }
            if(Character.isLowerCase(c)){
                isLower=true;
            }
            if(Character.isDigit(c)){
                isDigit=true;
            }
        }
        return isUpper && isLower && isDigit;
    }
}

答案 5 :(得分:0)

我个人认为您编写的代码太长,无法进行这么简单的检查。

但问题仍然是手可以修复,只有你可以放一个while循环来检查有效标志是否为真并且还在最后读取键盘输入。

类似下面的代码:

boolean valid = false;
while (!valid) {

        for (int i = 0; i < array.length; i++) {
            valid = true;
            if (!nu && !nl && !nd && !n7) {
                break;
            }

            if (array.length >= 7) // not < 7 cos breaks
            {
                n7 = false;
            }

            if (Character.isUpperCase(array[i])) {
                nu = false;
            }

            if (Character.isLowerCase(array[i])) {
                nl = false;
            }

            if (Character.isDigit(array[i])) {
                nd = false;
            }

        }

        if (n7) {
            valid = false;
            System.out
                    .println("Password does not contain 7 or more letters.");
        }
        if (nu) {
            valid = false;
            System.out
                    .println("Password does not contain an upper case letter.");
        }
        if (nl) {
            valid = false;
            System.out
                    .println("Password does not contain an lower case letter.");
        }
        if (nd) {
            valid = false;
            System.out.println("Password does not contain a number.");
        }
        if (!valid) {
            user_input = kb.nextLine();
            array = user_input.toCharArray();
        }
    }

答案 6 :(得分:0)

相反,即使有一个循环,你也可以使用String.matches()检查整个字符串。这将显着简化您的逻辑

喜欢这样

//For uppercase
user_input.matches(".*[A-Z].*"); // true if contians

//For lowercase
user_input.matches(".*[a-z].*"); // true if contians

//For digit
user_input.matches(".*\\d.*");  // true if contians