输出字母出现频率(Frequency of output letters)

要求1:输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位。 字母频率 = 这个字母出现的次数 / (所有A-Z,a-z字母出现的总数) 如果两个字母出现的频率一样,那么就按照字典序排列

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class piaopiao {

    public static void main(String[] args)throws IOException
    {
        List<Integer> list=new ArrayList<>();
        DecimalFormat df=new DecimalFormat("######0.00");    //格式化
//        File f = new File("D:\\飘英文版.txt");
         FileInputStream fip = new FileInputStream("D:\\《飘》英文版.txt");
        InputStreamReader reader = new InputStreamReader(fip, "gbk");
        StringBuffer sb = new StringBuffer();
        while (reader.ready()) {
            sb.append((char) reader.read());
        }
        //System.out.println(sb.toString());
        reader.close();
        fip.close();
        
        int i;
        String A=sb.toString();
        String M="abcdefghijklmnopqrstuvwxyz";
       String temp = "";
        char NUM[]=new char[A.length()];
        char Z[]=new char[26];
        int X[]=new int[26];
        int MAX=0;
        Z=M.toCharArray();
        for(int k=0;k<26;k++)
        {
            X[k]=0;
        for(i=0;i<A.length();i++)
        {
            NUM[i]=A.charAt(i);
            if(Z[k]==NUM[i]||Z[k]==ch(NUM[i]))
            {
                X[k]++;
            }
        }
        }
        //System.out.println("这篇文章中英文字母个数分别为:");
        
        double sum=0;
        //System.out.println("////////////排序如下:");
        for(i=0;i<25;i++)
            for(int k=0;k<25-i;k++)
            {
                if(X[k]<X[k+1])
                {
                    int temp2=X[k];
                    X[k]=X[k+1];
                    X[k+1]=temp2;
                    char temp3=Z[k];
                    Z[k]=Z[k+1];
                    Z[k+1]=temp3;
                }
            }
        for(i=0;i<26;i++)
        {
        //System.out.println(Z[i]+"字母个数为:"+X[i]);
        sum=sum+X[i];
        }
        for(i=0;i<26;i++)
        {
            double jkl=(X[i])/sum*100;
            System.out.println(Z[i]+"字母频率为:"+df.format(jkl)+"%");
        }
    }
    static char ch(char c)
    {
        if(!(c>=97&&c<=122))
            c+=32;
        return c;
    }
    
}
————————

Requirement 1: output the frequency of 26 letters in an English text file, arranged from high to low, and display the percentage of letters, accurate to two digits after the decimal point. Letter frequency = the number of occurrences of this letter / (the total number of occurrences of all A-Z and A-Z letters) if the two letters occur at the same frequency, they are arranged in dictionary order

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class piaopiao {

    public static void main(String[] args)throws IOException
    {
        List<Integer> list=new ArrayList<>();
        DecimalFormat df=new DecimalFormat("######0.00");    //格式化
//        File f = new File("D:\\飘英文版.txt");
         FileInputStream fip = new FileInputStream("D:\\《飘》英文版.txt");
        InputStreamReader reader = new InputStreamReader(fip, "gbk");
        StringBuffer sb = new StringBuffer();
        while (reader.ready()) {
            sb.append((char) reader.read());
        }
        //System.out.println(sb.toString());
        reader.close();
        fip.close();
        
        int i;
        String A=sb.toString();
        String M="abcdefghijklmnopqrstuvwxyz";
       String temp = "";
        char NUM[]=new char[A.length()];
        char Z[]=new char[26];
        int X[]=new int[26];
        int MAX=0;
        Z=M.toCharArray();
        for(int k=0;k<26;k++)
        {
            X[k]=0;
        for(i=0;i<A.length();i++)
        {
            NUM[i]=A.charAt(i);
            if(Z[k]==NUM[i]||Z[k]==ch(NUM[i]))
            {
                X[k]++;
            }
        }
        }
        //System.out.println("这篇文章中英文字母个数分别为:");
        
        double sum=0;
        //System.out.println("////////////排序如下:");
        for(i=0;i<25;i++)
            for(int k=0;k<25-i;k++)
            {
                if(X[k]<X[k+1])
                {
                    int temp2=X[k];
                    X[k]=X[k+1];
                    X[k+1]=temp2;
                    char temp3=Z[k];
                    Z[k]=Z[k+1];
                    Z[k+1]=temp3;
                }
            }
        for(i=0;i<26;i++)
        {
        //System.out.println(Z[i]+"字母个数为:"+X[i]);
        sum=sum+X[i];
        }
        for(i=0;i<26;i++)
        {
            double jkl=(X[i])/sum*100;
            System.out.println(Z[i]+"字母频率为:"+df.format(jkl)+"%");
        }
    }
    static char ch(char c)
    {
        if(!(c>=97&&c<=122))
            c+=32;
        return c;
    }
    
}