对《HarryPotter》的两个功能操作

功能一:

package harrypotter;

       import java.io.BufferedReader;

       import java.io.File;

       import java.io.FileReader;

       import java.text.DecimalFormat;

       public class HarryPotter1 {

              static double  a[][]=new double [52][2];

              //static char b[]=new char [52];

              static int number;

              public static void main(String[] args) {

                    

                     File file= new File("E:\java程序\Java24\src\harrypotter\Harry Potter and the Sorcerer's Stone.txt");

                     FileReader fr=null;

                     BufferedReader br=null;

                     try {

                            fr=new FileReader(file);

                            br=new BufferedReader(fr);

                            String  str=null;

                            while((str=br.readLine())!=null)

                            {

                                   //System.out.println(str);

                                   char [] c=str.toCharArray();

                                   //String b[]=new String[10000];

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

                                   {

                                          int x=Integer.valueOf(c[i]);

                                          if(x>=65&&x<=90)

                                          {

                                                 a[x-65][1]++;

                                                 number++;

                                          }

                                          else if(x>=97&&x<=122)

                                          {

                                                 a[x-97+26][1]++;

                                                 number++;

                                          }

                                   }

                                  

                            }

                            for(int j=0;j<52;j++)

                            {

                                   if(j<26)  a[j][0]=j+65;

                                   else a[j][0]=j+97-26;

                            }

                            for (int i = 0; i<51; i++)

                             for (int j = 0; j < 51- i; j++)

                             {

                                 

                                 if (a[j][1] < a[j + 1][1]) {

                                     double  temp = a[j][1];

                                     a[j][1]= a[j + 1][1];

                                     a[j + 1][1] = temp;

                                     double m = a[j][0];

                                     a[j][0]= a[j + 1][0];

                                     a[j + 1][0] = m;

                                 }

                             }

                             for (int j=0;j<52;j++)

                             {

                                   double b=a[j][1]/number*100;

                                    DecimalFormat df = new DecimalFormat("#.00");

                                   System.out.println((char)(int)a[j][0]+":"+df.format(b)+"%");

                             }

                           

                     } catch (Exception e) {

                            // TODO: handle exception

                           

                     }

              }

              private static String df(double b) {

                     // TODO 自动生成的方法存根

                     return null;

              }

       }

功能二:

package harrypotter;

import java.io.*;

import java.util.*;

import java.util.Map.Entry;

public class HarryPotter

{

        public static int n=0;

     public static void main(String[] args) {

     @SuppressWarnings("resource")

       Scanner input=new Scanner(System.in);

   

     int count=0;

     int num=1;

     String file1="E:\java程序\Java24\src\harrypotter\Harry Potter and the Sorcerer's Stone.txt1";

     try

     {

       BufferedReader a=new BufferedReader(new FileReader(file1));

       StringBuffer c=new StringBuffer();

       String s;

       while((s = a.readLine()) != null)

       {

             c.append(s);

       }

       String m=c.toString().toLowerCase();

       String [] d=m.split("[^a-zA-Z0-9]+");

       Map<String , Integer> myTreeMap=new  TreeMap<String, Integer>();

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

          

             if(myTreeMap.containsKey(d[i])) {

                 count = myTreeMap.get(d[i]);

                 myTreeMap.put(d[i], count + 1);

             }

             else {

                 myTreeMap.put(d[i], 1);

             }

         }

       List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(myTreeMap.entrySet());

     //按降序排序

       Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

          

             public int compare(Entry<String, Integer> k1, Entry<String, Integer> k2) {

                 return k2.getValue().compareTo(k1.getValue());

             }

            

         });

       System.out.println("请输入");

        System.out.println("请输入N的值:");

         n=input.nextInt();

       for(Map.Entry<String, Integer> map : list) {

             if(num <= n) {

       

                 System.out.println(map.getKey() + ":" + map.getValue());

                 num++;

             }

             else break;

         }

         a.close();

     }

     catch(FileNotFoundException e)

     {

       

     }

     catch(IOException e)

     {

       

     }

 }

 }

}

一,建立一个二维数组,通过a~z,A~Z的ASSIC码确定各字母在二维数组中的位置,然后利用冒泡排序法对各数值进行排序

      

原文地址:https://www.cnblogs.com/yangqqq/p/11808829.html