1. package wordscount; 
  2.  
  3. /** 
  4.  * 
  5.  * @author RaywayJohn  ----该程序为让用户输入字符串,并统计其中各个字符出现的次数 
  6.  */ 
  7.  
  8. import java.util.*; 
  9. public class Words 
  10.     String str; 
  11.     public Words( String str) 
  12.     { 
  13.         this.str=str; 
  14.     } 
  15.     public int count(char key)//计算某个字符是否在该字符串中,如果在返回出现的个数,如果不在,返回-1 
  16.     { 
  17.         int i=0
  18.         int temp=0
  19.         if(str.indexOf(key)!=-1
  20.         { 
  21.            for(i=str.indexOf(key);i<=str.lastIndexOf(key);i++) 
  22.            { 
  23.                if(key==str.charAt(i)) 
  24.                    temp=temp+1
  25.            } 
  26.            return temp; 
  27.         } 
  28.      else  return -1
  29.  
  30.     } 
  31.     public static void main(String[] args) 
  32.     { 
  33.         System.out.print("请输入字符串(可包含空格):"); 
  34.         Scanner input=new Scanner(System.in); 
  35.         Words wordsEng=new Words(input.nextLine()); 
  36.         int i=0
  37.         int j=0
  38.         int k=0
  39.         int temp=0
  40.         char tempChar[]=new char[wordsEng.str.length()];//该tempChar为保存之前计算过出现次数的某个字符 
  41.         char backSpace=32
  42.         tempChar[0]=wordsEng.str.charAt(0); 
  43.  a:   for(i=0;i<wordsEng.str.length();i++) 
  44.         { 
  45.        b:      for(j=0;j<i;j++) 
  46.              { 
  47.               if(tempChar[j]==wordsEng.str.charAt(i))//如果之前已经出现过该字符,那么不打印,如果没有出现过,则打印 
  48.                   continue a; 
  49.               else continue b; 
  50.             } 
  51.             if(wordsEng.str.charAt(i)!=32)//判断该字符受否为空格 
  52.                 System.out.println(wordsEng.str.charAt(i)+"出现的次数为:"+wordsEng.count(wordsEng.str.charAt(i))); 
  53.             else 
  54.                 System.out.println("空格出现的次数为:"+wordsEng.count(wordsEng.str.charAt(i))); 
  55.             tempChar[k++]=wordsEng.str.charAt(i); 
  56.        } 
  57.     }