亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復java中while循環的條件

如何修復java中while循環的條件

守著星空守著你 2023-08-04 15:28:42
我的代碼很簡單;檢查字符串中有多少數字、小寫字母、大寫字母和特殊字符,但每種字符必須至少有一個;我認為 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 類型時它都會要求提供字符串,但事實并非如此
查看完整描述

3 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

boolean嘗試從方法返回狀態checkPass并在方法中放置一個 while 循環main,狀態將是您正在檢查的條件。


這樣,如果輸入的字符串通過驗證,您可以中斷 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;

 }


查看完整回答
反對 回復 2023-08-04
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

我建議使用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;

}


查看完整回答
反對 回復 2023-08-04
?
呼如林

TA貢獻1798條經驗 獲得超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("密碼"); 輸出==> {正常上=2,正常下=6}


checkPass("P@ss@Wo1r d3"); 輸出==> {特殊=3,數字=2,正常上=2,正常下=5}


查看完整回答
反對 回復 2023-08-04
  • 3 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號