當前位置:編程學習大全網 - 網站源碼 - 怎樣用java寫這個程序:定義壹個函數,用於統計輸入字符串中數字字符的個數

怎樣用java寫這個程序:定義壹個函數,用於統計輸入字符串中數字字符的個數

public class StringUtils001 {

public static void main(String[] args) {

String input = getInputString();

int numCount = countNum(input);

JOptionPane.showMessageDialog(null, "字符串 [" + input + "] 中數字的個數為:" + numCount );

}

/** 計算字符串中數字的個數 */

public static int countNum(String str){

int count = 0;

for(char c:str.toCharArray()){

if(Character.isDigit(c)){

count++;

}

}

return count;

}

private static String getInputString() {

String input = null;

while(true){

input = JOptionPane.showInputDialog("請輸入字符串");

if(input == null || input.trim().length() == 0)

JOptionPane.showMessageDialog(null, "忽悠我是吧, 別正個空的字符串啊 ~");

else

return input;

}

}

}

  • 上一篇:青盈科技煙盒回收怎麽樣
  • 下一篇:美圖m6手機是否支持USBType-C接口
  • copyright 2024編程學習大全網