【问题标题】:how to fix my conditions for the while loop in java如何在java中修复while循环的条件
【发布时间】: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


【解决方案1】:

尝试从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;
 }

【讨论】:

  • 次要的挑剔,但没有太多理由在循环之前将其声明为 boolean flag,因为您可以稍后执行 boolean flag = ... 并返回它,因为您实际上并没有将它用于其他任何事情。或者更好的是只返回布尔值而不使用return normalLower == 0 || ... 创建局部变量
  • @Nexevis 是的,我同意,谢谢!
【解决方案2】:

作为@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;
}

【讨论】:

  • 作为建议,由于您现在使用char 来检查值,因此您可以利用使用for (char c : str.toCharArray()){} 的增强型for 循环,它也允许您删除char c = str.charAt(i)
【解决方案3】:

这是一个使用 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("密码"); 输出 ==> {normalupper=2, normalLower=6}

checkPass("P@ss@Wo1r d3"); 输出 ==> {special=3, number=2, normalupper=2, normalLower=5}

【讨论】:

    猜你喜欢
    • 2015-09-29
    • 2016-03-30
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2017-02-25
    相关资源
    最近更新 更多