眷天

随便敲

package total;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class Statistics_letter {

    public static void main(String[] args) throws IOException  {
        // TODO Auto-generated method stub
        FileReader fr=new FileReader("a.txt");
        BufferedReader bufr=new BufferedReader(fr);
        StringBuffer str=new StringBuffer();
        String Line=null;
        while((Line=bufr.readLine())!=null) {
            str.append(Line);
        }
        bufr.close();
        
        double capitalletter[]=new double[26];
        double lowercaseletter[]=new double[26];
        int count=0;
        for(int i=0;i<str.length();i++) {
            char ch=str.charAt(i);
            if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') {
                for(int j=0;j<26;j++) {
                    if(ch=='A'+j)
                    capitalletter[j]++;
                }
                for(int k=0;k<26;k++) {
                    if(ch=='a'+k)
                        lowercaseletter[k]++;
                }
                count++;    
            }
        }
        
        double percentage1[]=new double[52];
        double percentage2[]=new double[52];
        for(int i=0;i<26;i++) {
            percentage1[i]=capitalletter[i]/count;
            percentage2[i]=percentage1[i];
        }
        for(int i=26;i<52;i++) {
            percentage1[i]=lowercaseletter[i-26]/count;
            percentage2[i]=percentage1[i];
        }
        Arrays.sort(percentage1);
        for(int i=51;i>=0;i--) {
            int max=0;
            for(int j=0;j<52;j++) {
                if(percentage2[j]==percentage1[i])
                    max=j;
            }
            if(max>=26)
                System.out.print(((char)('a'+max-26))+":");
            else
            System.out.print(((char)('A'+max))+":");
            System.out.println(String.format("%.2f",percentage1[i]*100)+'%');
        }
        System.out.println("英文字母总数为:"+count);
    }
}

posted on 2021-12-31 12:18  眷天  阅读(25)  评论(0编辑  收藏  举报

导航