好的,所以这很长。我正在做一个课堂项目,我们得到了如何做的例子。我对此很陌生,所以我做了我认为可以做的事情,但我似乎无法做到这一点,并且可能在这段代码中有大量错误和不正确的东西。这是一个密码认证系统,只是其中的一半。我注释掉了生成密码功能,因为我还没有到那里。整个程序编译,但它总是生成密码不正确。
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 个数字,它会根据大小写显示“您输入的密码有效或无效”。这只每次都显示无效。