-1

我想要做的是从用户那里获取密码,然后检查它的 4 个条件。如果是对的。然后打印出“密码正确”。如果不是,请向用户显示问题所在并返回菜单。问题是我无法返回菜单。如果我在 line 中做一个 while 循环//User interface //suppose to put a while loop here, but..... System.out.println(); System.out.println("Please create a password and a legal one"。它确实会返回,但某些“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.");


    }
}
4

7 回答 7

2

在您当前的代码中,您需要确保在主循环的每次迭代中都重置nu, nl, nd, 。n7

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.");
于 2013-09-19T04:01:55.110 回答
0

while循环从以下开始有什么问题:

String user_input;      // To hold input

并在之前结束:

        System.out.println("Password is correct.");
于 2013-09-19T03:54:30.920 回答
0

为什么你应该去那个特定的原因你已经显示了密码应该是怎样的内容。

只需打印“密码不符合上述条件请重试”

我觉得够了

于 2013-09-19T04:04:08.553 回答
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将保持该值。

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

于 2013-09-19T04:06:15.450 回答
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;
    }
}
于 2013-09-19T04:06:50.683 回答
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();
        }
    }
于 2013-09-19T04:10:38.307 回答
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
于 2013-09-19T04:11:38.923 回答