zoukankan      html  css  js  c++  java
  • 对《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码确定各字母在二维数组中的位置,然后利用冒泡排序法对各数值进行排序

          

  • 相关阅读:
    图文并茂记录下重新配置Win10系统Flutter环境--内含Android Studio 下载安装教程
    图文并茂解决Client does not support authentication protocol requested by server; consider upgrading MySQL
    图文并茂基于阿里云linux服务器部署nodejs项目并添加pm2守护nodejs项目运行进程(Linux version 4.19.81-17.1.al7.x86_64)
    超简单的图文并茂Linux上使用yum安装Mysql(Aliyun Linux release 2.1903 LTS)
    使用linux命令直接在网上下载文件,解压,改名
    解决使用linux部署nodejs服务测试代码返回中文是乱码
    Echarts点击多组数据多个柱子中的一个柱子,获取当前点击的是第几组数据,并获取点击的是当前组别第几根柱子,以及对应横坐标,
    flutter 2.X报错 Bad state: Insecure HTTP is not allowed by platform:
    flutter 2.x运行flutter run 报错Cannot run with sound null safety, because the following dependenciesdon'
    flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.
  • 原文地址:https://www.cnblogs.com/yangqqq/p/11808829.html
Copyright © 2011-2022 走看看