0

好的,所以这很长。我正在做一个课堂项目,我们得到了如何做的例子。我对此很陌生,所以我做了我认为可以做的事情,但我似乎无法做到这一点,并且可能在这段代码中有大量错误和不正确的东西。这是一个密码认证系统,只是其中的一半。我注释掉了生成密码功能,因为我还没有到那里。整个程序编译,但它总是生成密码不正确。

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

       // if (args.length == 0) // Generate password
        //{

        //System.out.println("Your new password is: " + generateValidPassword());

        //} 
        if (args.length == 1) // Test password
        {          
            System.out.print("The password you entered, " + args[0]);

            if (isValidPassword(args[0]))
            {
                System.out.println(" is a valid password");
            }
            else
            {
                System.out.println(" is NOT a valid password");
            }    
        }
        else // To many command line arguments, educate user
        {

            System.out.println();
            System.out.println("This program takes either zero or one parameter.");
            System.out.println("When run with zero parameters it will generate a password.");
            System.out.print("When run with one parameter it will test whether"); 
            System.out.println(" the parameter is a valid password.");
            System.out.println();
            System.out.println("A valid password has the following four properties:");
            System.out.println("1. A valid password contains between 8 and 14 characters, inclusive.");
            System.out.println("2. A valid password contains two or more uppercase letters [A-Z].");
            System.out.println("3. A valid password contains two or more lowercase letters [a-z]");
            System.out.println("4. A valid password contains two or more numbers [0-9]");
            System.out.println();
            System.out.println();
        }
   } 

static private boolean isValidPassword(String testPassword) 
 { 
     boolean returnValue = false;
     int uppercasecount = 0;
     int lowercasecount = 0;
     int numbercount = 0;


    if(uppercasecount>=2&&lowercasecount>=2&&numbercount>=2)
    {
        returnValue = true;
    }
    return returnValue;
}
    static public boolean CharacterIsNumber(char testChar) 
    { 
        int numbercount = 0;
        boolean returnValue = false;
        switch (testChar)
        {
            case '0':
                returnValue = true;
                //numbercount++;
                break;
            case '1':
                returnValue = true;
                //numbercount++;
                break;
            case '2':
                returnValue = true;
                //numbercount++;
                break;
            case '3':
                returnValue = true;
                //numbercount++;
                break;
            case '4':
                returnValue = true;
                //numbercount++;
                break;
            case '5':
                returnValue = true;
                //numbercount++;
                break;            
            case '6':
                returnValue = true;
                //numbercount++;
                break;
            case '7':
                returnValue = true;
                //numbercount++;
                break;
            case '8':
                returnValue = true;
                //numbercount++;
                break;
            case '9':
                returnValue = true;
                //numbercount++;
                break;           
        }
        numbercount++;
        return returnValue;
    }
    static public boolean CharacterIsUppercase(char testChar) 
    {
        int uppercasecount = 0;
        boolean returnValue = false;

        switch (testChar)
            {
                case 'A':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'B':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'C':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'D':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'E':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'F':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'G':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'H':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'I':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'J':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'K':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'L':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'M':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'N':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'O':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'P':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'Q':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'R':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'S':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'T':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'U':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'V':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'W':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'X':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'Y':
                    returnValue = true;
                    //uppercasecount++;
                    break;
                case 'Z':
                    returnValue = true;
                    //uppercasecount++;
                    break;
            }

        uppercasecount++;
        return returnValue;
    }
    static public boolean CharacterIsLowercase(char testChar) 
    {
        int lowercasecount = 0;
        boolean returnValue = false;

        switch (testChar)
            {
                case 'a':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'b':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'c':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'd':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'e':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'f':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'g':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'h':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'i':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'j':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'k':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'l':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'm':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'n':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'o':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'p':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'q':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'r':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 's':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 't':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'u':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'v':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'w':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'x':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'y':
                    returnValue = true;
                    //lowercasecount++;
                    break;
                case 'z':
                    returnValue = true;
                    //lowercasecount++;
                    break;
            }

        lowercasecount++;
        return returnValue;
    }
}           

我注释掉了几个不同的部分,例如大写计数++ 函数,因为我尝试了不同的方法,但都不起作用。这部分应该检查密码是否有 2 个大写、2 个小写和 2 个数字,它会根据大小写显示“您输入的密码有效或无效”。这只每次都显示无效。

4

1 回答 1

0

您的代码的问题来自方法 *isValidPassword()**。您正在检查它是否符合条件,但您从未增加变量。我已更新您的代码以缩短它。此外,它从您的代码中删除了错误。将您的代码与此代码进行比较。此外,此代码不起作用。我只修复了错误并稍微缩短了您的代码。

package test;

/**
 *
 * @author Tyler Weaver
 */
public class Test {

    public static void main(String[] args) {
        // if (args.length == 0) // Generate password
        //{

        //System.out.println("Your new password is: " + generateValidPassword());
        //} 
        if (args.length == 1) // Test password
        {
            System.out.print("The password you entered, " + args[0]);

            if (isValidPassword(args[0])) {
                System.out.println(" is a valid password");
            } else {
                System.out.println(" is NOT a valid password");
            }
        } else // To many command line arguments, educate user
        {

            System.out.println();
            System.out.println("This program takes either zero or one parameter.");
            System.out.println("When run with zero parameters it will generate a password.");
            System.out.print("When run with one parameter it will test whether");
            System.out.println(" the parameter is a valid password.");
            System.out.println();
            System.out.println("A valid password has the following four properties:");
            System.out.println("1. A valid password contains between 8 and 14 characters, inclusive.");
            System.out.println("2. A valid password contains two or more uppercase letters [A-Z].");
            System.out.println("3. A valid password contains two or more lowercase letters [a-z]");
            System.out.println("4. A valid password contains two or more numbers [0-9]");
            System.out.println();
            System.out.println();
        }
    }

    static private boolean isValidPassword(String testPassword) {
        int uppercasecount = 0;
        int lowercasecount = 0;
        int numbercount = 0;

        for (int count = 0; count < testPassword.length(); count++) {
            char cur = testPassword.charAt(count);

            if (CharacterIsNumber(cur)) {
                numbercount++;
            } else if (CharacterIsUppercase(cur)) {
                uppercasecount++;
            } else if (CharacterIsLowercase(cur)) {
                lowercasecount++;
            }
        }

        return (uppercasecount >= 2 && 
                lowercasecount >= 2 &&
                numbercount >= 2);
    }

    static public boolean CharacterIsNumber(char testChar) {
        return Character.isDigit(testChar);
    }

    static public boolean CharacterIsUppercase(char testChar) {
        return Character.isUpperCase(testChar);
    }

    static public boolean CharacterIsLowercase(char testChar) {
        return Character.isLowerCase(testChar);
    }
}

附带说明:除非您不使用 IDE,否则我会避免使用 args[] 命令行。如果使用 IDE,它不会总是正常工作。如果您是新手,我建议您使用 Scanner 课程。

于 2014-10-20T00:29:49.170 回答