首页 > 其他 > 详细

统计小说《飘》中英文单词

时间:2019-09-27 02:08:26      阅读:118      评论:0      收藏:0      [点我收藏+]
package class20190923;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.StringTokenizer;
public class Classtext2 {
    private static String str1;
    private static String[] str3 = new String[20000];
    private static int[] n=new int[20000];
    
    public static void main(String[] args) throws IOException {
        
        Scanner sc=new Scanner(System.in);
        File file = new File("src/飘英文版.txt");
        String tempstr=null;
        BufferedReader reader = null;
        reader = new BufferedReader(new FileReader(file));
        int i=0,sum=0,j=0;
        while((tempstr=reader.readLine())!=null) {
        StringTokenizer st = new StringTokenizer(tempstr," .,?”“;:‘‘   !—‘");
        while(st.hasMoreElements()) {
        str1=(String)st.nextElement();
        str1=str1.toLowerCase();
        for(i=0;i<=sum;i++)
        {
            if(str1.equals(str3[i])) {
                n[i]++;
                break;}
        }
        if(i>sum) {
            str3[sum]=str1;
            n[sum]=1;
            sum++;
            }
                }
                    }
        
        for( i=0;i<sum;i++) {
            for( j=i+1;j<sum;j++) {
                if(n[j]>n[i]) {
                    int temp=n[i];
                    n[i]=n[j];
                    n[j]=temp;
                    String Temp=str3[i];
                    str3[i]=str3[j];
                    str3[j]=Temp;
                }
            }
        }

        System.out.print("你要前几个最多的单词:");
         int choose = sc.nextInt();
         for(i=0;i<choose;i++) {
             System.out.println(str3[i]+"    "+n[i]);
         }
    }
}

结果:

你要前几个最多的单词:5
the
and
to
of
her

统计小说《飘》中英文单词

原文:https://www.cnblogs.com/wuren-best/p/11595046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!