如何在java中修复while循环的条件
我的代码很简单;检查一个字符串中有多少数字、小写字母、大写字母和特殊字符,但必须每个字符至少有一个 我认为while循环中的AND或or条件存在问题如何在java中修复while循环的条件,java,string,while-loop,infinite-loop,logical-operators,Java,String,While Loop,Infinite Loop,Logical Operators,我的代码很简单;检查一个字符串中有多少数字、小写字母、大写字母和特殊字符,但必须每个字符至少有一个 我认为while循环中的AND或or条件存在问题 public static void main(String[] args) { Scanner scn = new Scanner(System.in); String name = scn.nextLine(); checkPass(name); } public static void ch
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);
}
publicstaticvoidmain(字符串[]args){
扫描仪scn=新扫描仪(System.in);
字符串名称=scn.nextLine();
支票通行证(姓名);
}
公共静态无效检查传递(字符串str){
int toul=str.length();
int normalLower=0;
int-normalUpper=0;
整数=0;
int-special=0;
while(normalLower==0 | | normalUpper==0 | | number==0 | | special==0){
for(int i=0;i
我希望每当缺少字符类型时,它都会请求字符串,但它不会尝试从
checkPass
方法返回boolean
状态,并在main
方法中放入while循环,状态将是您正在检查的条件
这样,如果输入的字符串通过验证,则可以中断while循环,否则循环将继续请求有效的输入字符串
:
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
String name = scn.nextLine();
while(checkPass(name)){
name = scn.nextLine();
}
}
// If the boolean retuned from this method is false it will break the while loop in main
public static boolean checkPass(String str) {
int toul = str.length();
int normalLower = 0;
int normalUpper = 0;
int number = 0;
int 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);
return normalLower == 0 || normalUpper == 0 || number == 0 || special == 0;
}
publicstaticvoidmain(字符串[]args)引发异常{
扫描仪scn=新扫描仪(System.in);
字符串名称=scn.nextLine();
while(checkPass(name)){
name=scn.nextLine();
}
}
//如果从该方法返回的布尔值为false,它将中断main中的while循环
公共静态布尔校验传递(字符串str){
int toul=str.length();
int normalLower=0;
int-normalUpper=0;
整数=0;
int-special=0;
for(int i=0;i
作为@Fullstack Guy answer的更新,我建议使用Character类检查我们处理的字符类型:
public static boolean checkPass(String str) {
int normalLower=0;
int normalUpper=0;
int number=0;
int special=0;
for (char c : str.toCharArray()) {
if (Character.isDigit(c)) {
number++;
} else if (Character.isUpperCase(c)) {
normalUpper++;
} else if (Character.isLowerCase(c)) {
normalLower++;
} else {
special++;
}
}
System.out.println("normalupper " + normalUpper);
System.out.println("normallower " + normalLower);
System.out.println("number" + number);
System.out.println("special " + special);
return normalLower == 0 || normalUpper == 0 || number == 0 || special == 0;
}
以下是使用Java 8 Streams和lambda函数获取计数的版本:
public static String getType(int code){
if(Character.isDigit(code)) return "number";
if(Character.isLowerCase(code)) return "normalLower";
if(Character.isUpperCase(code)) return "normalupper";
return "special";
}
public static void checkPass(String s){
Map map =s.chars().mapToObj(x->getType(x))
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println(map);
}
样本运行:
检查通行证(“密码”);输出==>{normalupper=2,normalLower=6}
支票通行证(“P@ss@Wo1r d3”);输出==>{special=3,number=2,normalupper=2,
normalLower=5}
次要的吹毛求疵,但这并不是在循环之前将其声明为
boolean flag
的理由,因为您可以稍后执行boolean flag=…
并返回它,因为您实际上不将其用于任何其他用途。或者更好的方法是只返回布尔值,而不创建局部变量,使用return normalLower==0 | |…
@Nexevis是的,我同意,谢谢!作为建议,由于您现在使用char
来检查值,因此您可以利用增强的for
循环,使用for(char c:str.toCharArray()){}
也可以删除char c=str.charAt(i)
,如果您的密码与任何条件不匹配,将导致无限循环