我被分配的程序有以下规则:
规则:
- 字符数必须在 [6, 10]
2. 范围内必须有 >= 2 个字符 ['a', 'z']
3. 范围内必须有 >= 2 个字符 ['A', 'Z']
- ['0', '9'] 范围内必须有 >= 2 个字符
例如,pw1("aaBC09-+") 应该返回 true,而 pw1("aabC09-+") 应该返回 false。
我附上了我的代码,但我可以使用更高效/更简洁的代码吗?
import java.util.*;
public class PassChecker2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt User to Enter String
System.out.print("Enter Password: ");
// Grab User Generated String
String pw = input.next();
// Pass String to be Checked
boolean valid_pw = checkPW(pw);
if(valid_pw)
System.out.println(pw + " is a valid password!");
else
System.out.println(pw + " is not a valid password!");
}
public static boolean checkPW(String pw)
{
int pwLowerCase = 0,
pwUpperCase = 0,
pwNumber = 0;
// Return False if PW is too Small / Large
if(!(pw.length() >= 2 && pw.length() <= 10))
return false;
for(int position = 0; position < pw.length(); ++position)
{
char character = pw.charAt(position);
if(character >= 'a' && character <= 'z')
++pwLowerCase;
else if(character >= 'A' && character <= 'Z')
++pwUpperCase;
else if(character >= '1' && character <= '9')
++pwNumber;
}
return (pwLowerCase >= 2 && pwUpperCase >= 2 && pwNumber >= 2);
}
}