統計一個字符串中大寫字母,小寫字母,以及數字的個數。

public class StringTest {
 public static void main(String[] args) {
  // 封裝鍵盤錄入數據
  Scanner sc = new Scanner(System.in);
  System.out.println("請輸入一個字符串:");
  String line = sc.nextLine();

  // 定義統計變量
  int big = 0;
  int small = 0;
  int number = 0;

  // length,charAt
  for (int x = 0; x < line.length(); x++) {
   // 獲取每一個字符
   char ch = line.charAt(x);
   if (ch >= '0' && ch <= '9') {
    number++;
   } else if (ch >= 'A' && ch <= 'Z') {
    big++;
   } else if (ch >= 'a' && ch <= 'z') {
    small++;
   }
  }
  
  System.out.println("字符串中大寫字母出現次數是:"+big);
  System.out.println("字符串中小寫字母出現次數是:"+small);
  System.out.println("字符串中數字出現次數是:"+number);
  
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章