Blueherb In solitude, where we are least alone

java 倒入一篇文章,显示每个字母的出现概率,从大到小排序

 1 package com_1;
 2 import java.io.BufferedReader;
 3 import java.io.FileReader;
 4 import java.io.IOException;
 5 import java.math.BigInteger;
 6 import java.text.DecimalFormat;
 7 import java.util.ArrayList;
 8 import java.util.Comparator;
 9 import java.util.TreeMap;
10 import java.util.TreeSet;
11 public class Shuru2
12 {
13     public static void main(String [] args) throws IOException {
14         BufferedReader br=new BufferedReader(new FileReader("F:\\eclipse-workspace\\com_1\\src\\1.txt"));        
15         TreeMap<Character,Integer> hm=new TreeMap<>();
16         int  bb;
17         while((bb=br.read())!=-1) {    
18              if((bb>='A'&&bb<='Z')||(bb>'a'&&bb<='z')) {
19                  hm.put((char)bb,hm.containsKey((char)bb)?hm.get((char)bb)+1:1);
20             }
21             
22             }    
23         br.close();
24         int max=0;
25         int sum=0;
26         int t=0;
27         for(Character k: hm.keySet()) {
28             sum=sum+hm.get(k);
29           }
30         TreeSet<Character> ts=new TreeSet<>(new Comparator<Character>()
31         {
32             public int compare(Character a,Character b) {
33                 int num=hm.get(a)-hm.get(b);
34                 return num==0?1:(-num);
35             }
36         });
37         for(Character k: hm.keySet()) {
38             ts.add(k);
39         }
40         DecimalFormat df = new DecimalFormat("0.00%");
41         for (Character c : ts)
42         {
43             float bai=(float)hm.get(c)/sum;
44            
45             System.out.println(c+" "+hm.get(c)+" "+df.format(bai));
46         }
47           
48         
49         
50         //System.out.println(sum);
51 }}

整个人都不好了

posted @ 2019-11-04 20:56  帅气的小土豆  阅读(248)  评论(0编辑  收藏  举报