java-統計字符串中各字符次數


package com.day5.test;

public class Test2 {

  /**
  * @param args
  * 需求:統計字符串中大寫字母字符,小寫字母字符,數字字符出現的次數,其他字符出現的次數
  * ABCDEabcd123456!@#$%^

    注意if..else if...else與if...if...if...else的區別
  */
public static void main(String[] args) {
  String s="ABCDEabcd123456!@#$%^&";
  int big=0;
  int small=0;
  int num=0;
  int other=0;
  /*for(int i=0;i<s.length();i++)
  {
    char c=s.charAt(i);
    if(c>='A'&&c<='Z')
      big++;
    if(c>='a'&&c<='z')
      small++;
    if(c>='0'&&c<='9')//注意不能寫成if(c>=0&&c<=9)
      num++;
    else
      other++;
  }
  System.out.println(big);//5
  System.out.println(small);//4
  System.out.println(num);//6
  System.out.println(other);//16
  */
  for(int i=0;i<s.length();i++)
  {
    char c=s.charAt(i);
    if(c>='A'&&c<='Z')
      big++;
    else if(c>='a'&&c<='z')
      small++;
    else if(c>='0'&&c<='9')//注意不能寫成if(c>=0&&c<=9)
      num++;
    else
      other++;
  }
  System.out.println(big);//5
  System.out.println(small);//4
  System.out.println(num);//6
  System.out.println(other);//7
  }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM