【发布时间】:2020-01-18 16:29:54
【问题描述】:
我的代码很简单;检查一个字符串有多少数字,小写字母,大写字母和特殊字符,但它必须至少有一个;
我认为我的 while 循环中的条件的 AND 或 OR 存在问题
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String name = scn.nextLine();
checkPass(name);
}
public static void checkPass (String str){
int toul = str.length();
int normalLower=0;
int normalUpper=0;
int number=0;
int special=0;
while(normalLower==0 || normalUpper==0 || number==0 || special==0) {
for (int i = 0; i < toul; i++) {
String s = String.valueOf(str.charAt(i));
if (s.matches("^[a-z]*$")) {
normalLower++;
} else if (s.matches("^[A-Z]*$")) {
normalUpper++;
} else if (s.matches("^[0-9]*$")) {
number++;
} else {
special++;
}
}
}
System.out.println("normalupper " + normalUpper);
System.out.println("normallower " + normalLower );
System.out.println("number" + number);
System.out.println("special " + special);
}
我希望它在缺少 char 类型时请求一个字符串,但事实并非如此
【问题讨论】:
-
嗨 moez daly,如果您的密码不符合任何条件,则会导致无限循环
标签: java string while-loop infinite-loop logical-operators