繁体   English   中英

如何解决我在Java中的while循环条件

[英]how to fix my conditions for the while loop in java

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

我认为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类型时它都会要求一个字符串,但是它很简单

尝试从checkPass方法返回boolean状态,并在main方法中放置while循环,该状态将成为您要检查的条件。

这样,如果输入的字符串通过您的验证,则可以中断while循环,否则该循环将继续询问有效的输入String

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;
 }

作为@Fullstack Guy答案的更新,我建议使用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);
}

样品运行:

checkPass(“ PassWord”); 输出==> {normalupper = 2,normalLower = 6}

checkPass(“ P @ ss @ Wo1r d3”); 输出==> {特殊= 3,数字= 2,normalupper = 2,normalLower = 5}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM