Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中修复while循环的条件_Java_String_While Loop_Infinite Loop_Logical Operators - Fatal编程技术网

如何在java中修复while循环的条件

如何在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

我的代码很简单;检查一个字符串中有多少数字、小写字母、大写字母和特殊字符,但必须每个字符至少有一个

我认为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);
}
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)
,如果您的密码与任何条件不匹配,将导致无限循环