輸入一串字符,算出各種字符的個數--簡單


源程序:

import java.util.Scanner;

public class p78_10 {

  public static void main(String[] args)

  {

    int n_char=0,n_digit=0,n_other=0;

    char []a=new char[100];

    Scanner reader=new Scanner(System.in);

    System.out.print("請輸入一串字符(包含字母、數字和特殊字符):");

    String str1=reader.next();

    for(int i=0;i<str1.length();i++)

    {

      a[i]=str1.charAt(i);

      if(a[i]>='a'&&a[i]<='z' || a[i]>='A'&&a[i]<='Z')

        n_char++;

      else if(a[i]>='0'&&a[i]<='9')

        n_digit++;

      else

        n_other++; 

    }

    System.out.println("字母的個數:"+n_char+" ");

    System.out.println("數字的個數:"+n_digit+" ");

    System.out.println("特殊字符的字母的個數:"+n_other+" ");

    System.out.println("全部字符個數:"+str1.length()+" ");

    reader.close();

  } 

}

 

 運行結果:

 


免責聲明!

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



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