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

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


我认为 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 类型时它都会要求提供字符串,但事实并非如此


守着星空守着你
浏览 145回答 3
3回答

沧海一幻觉

boolean尝试从方法返回状态checkPass并在方法中放置一个 while 循环main,状态将是您正在检查的条件。这样,如果输入的字符串通过验证,您可以中断 while 循环,否则循环将继续要求有效输入String:public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Scanner scn = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; String name = scn.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; while(checkPass(name)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = scn.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;// If the boolean retuned from this method is false it will break the while loop in main&nbsp;public static boolean checkPass(String str) {&nbsp; &nbsp; &nbsp;int toul = str.length();&nbsp; &nbsp; &nbsp;int normalLower = 0;&nbsp; &nbsp; &nbsp;int normalUpper = 0;&nbsp; &nbsp; &nbsp;int number = 0;&nbsp; &nbsp; &nbsp;int special = 0;&nbsp; &nbsp; &nbsp;for (int i = 0; i < toul; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String s = String.valueOf(str.charAt(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (s.matches("^[a-z]*$")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;normalLower++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if (s.matches("^[A-Z]*$")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;normalUpper++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if (s.matches("^[0-9]*$")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;number++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;special++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println("normalupper " + normalUpper);&nbsp; &nbsp; &nbsp; System.out.println("normallower " + normalLower);&nbsp; &nbsp; &nbsp; System.out.println("number" + number);&nbsp; &nbsp; &nbsp; System.out.println("special " + special);&nbsp; &nbsp; &nbsp; return normalLower == 0 || normalUpper == 0 || number == 0 || special == 0;&nbsp;}

杨__羊羊

我建议使用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){&nbsp; &nbsp; if(Character.isDigit(code)) return "number";&nbsp; &nbsp; if(Character.isLowerCase(code)) return "normalLower";&nbsp; &nbsp; if(Character.isUpperCase(code)) return "normalupper";&nbsp; &nbsp; return "special";}public static void checkPass(String s){&nbsp; &nbsp; Map map =s.chars().mapToObj(x->getType(x))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));&nbsp; &nbsp; System.out.println(map);}示例运行:checkPass("密码"); 输出==> {正常上=2,正常下=6}checkPass("P@ss@Wo1r d3"); 输出==> {特殊=3,数字=2,正常上=2,正常下=5}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java