【发布时间】:2015-02-14 22:19:14
【问题描述】:
我目前正在尝试使用布尔方法使密码验证器工作,因为老师要求我们这样做。这让我发疯。要正确,密码需要有一个大写字母、一个小写字母、至少 10 个字符和一个数字。我知道现在,我的方法完全返回值为 false,但我想知道一旦我有一个大写或一个小写,我怎么能破坏代码。
非常感谢您的帮助!
public class AtLeast1UppercaseLowercaseNumber {
public static void main(String[] args){
String password = "H";
System.out.println(password);
if(isSecurePassword(password)){
System.out.println("Yay it works");}
else {
System.out.println("you suck");}
}
public static isSecurePassword(String password) {
int uppercase = 0, lowercase = 0, number = 0;
for(int i=0; i<password.length(); i++) {
for(char c ='A'; c <='Z'; c++) {
if(password.charAt(i) == c) {
uppercase++;
if( uppercase >= 1) {
for(char t = 'a'; t <='z'; t++) {
if(password.charAt(i) == t) {
lowercase++;
if(lowercase >= 1) {
}
}
}
for(int j = '0'; j <='9'; j++) {
if(password.charAt(i) == j) {
number++;
if( number >= 1) {
}
}
}
}
return false;
}
}
【问题讨论】:
-
这不是一个真正获得家庭作业帮助的地方。您是否查看过使用字符串、检查内容和长度的其他方法?想想许多循环是否真的是正确的方法(以及它理解起来有多复杂)。此外,请在提问时具体说明要求和对问题的描述。