• JAVA课堂测试之查找字母和单词出现次数、频率


    代码如下:没有加注释,自己研究吧。

    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 test2 {
    public static void main(String[] args)throws IOException
    {
    List<Integer> list=new ArrayList<>();
    DecimalFormat df=new DecimalFormat("######0.00");
    File f = new File("piao1.txt");
    FileInputStream fip = new FileInputStream(f);
    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)+"%");
    }
    StringTokenizer st = new StringTokenizer(sb.toString(),",.! ");
    String []a1=StatList(sb.toString());
    int[]b1=StatList1(sb.toString());
    System.out.println("//////////////////////////////");
    for(i=0;i<a1.length-1;i++)
    for(int j=0;j<a1.length-1-i;j++)
    {
    if(b1[j]<b1[j+1])
    {
    int temp6=b1[j];
    b1[j]=b1[j+1];
    b1[j+1]=temp6;
    String temp7=a1[j];
    a1[j]=a1[j+1];
    a1[j+1]=temp7;
    }
    }
    for(i=0;i<a1.length-1;i++)
    {
    System.out.println("单词:"+a1[i]+" 且出现的次数:"+b1[i]);
    }
    }
    static char ch(char c)
    {
    if(!(c>=97&&c<=122))
    c+=32;
    return c;
    }
    static String[] StatList(String str) {
    StringBuffer sb = new StringBuffer();
    HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
    String[] slist = str.split("\W+");
    int sum=0;
    int sum1=0;
    for (int i = 0; i < slist.length; i++) {
    if (!has.containsKey(slist[i])) { // 若尚无此单词
    has.put(slist[i], 1);
    sum++;
    sum1++;
    } else {//如果有,就在将次数加1
    Integer nCounts = has.get(slist[i]);

    has.put(slist[i],nCounts+1 );
    }
    }
    int temp=0;
    int temp1=0;
    String []a=new String[sum];
    int []b=new int[sum1];
    Iterator iterator = has.keySet().iterator();
    while(iterator.hasNext()){
    String word = (String) iterator.next();
    a[temp]=word;
    temp++;
    }
    return a;
    }
    static int[] StatList1(String str) {
    StringBuffer sb = new StringBuffer();
    HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
    String[] slist = str.split("\W+");
    int sum=0;
    int sum1=0;
    for (int i = 0; i < slist.length; i++) {
    if (!has.containsKey(slist[i])) { // 若尚无此单词
    has.put(slist[i], 1);
    sum++;
    sum1++;
    } else {//如果有,就在将次数加1
    Integer nCounts = has.get(slist[i]);

    has.put(slist[i],nCounts+1 );

    }
    }
    int temp=0;
    int temp1=0;
    String []a=new String[sum];
    int []b=new int[sum1];
    Iterator iterator = has.keySet().iterator();
    while(iterator.hasNext()){
    String word = (String) iterator.next();
    b[temp1]=has.get(word);
    temp1++;
    }
    return b;
    }
    }

  • 相关阅读:
    Android 在一个程序中启动另一个程序
    Android SDK Manager国内无法更新的解决方案
    Android studio 安装中遇到一些问题的解决办法,分享一下
    apache服务器伪静态配置说明
    POJ3253 Fence Repair【贪心】
    洛谷P1090 合并果子【贪心】
    POJ3069 Saruman's Army【贪心】
    洛谷P1012 拼数【字符串+排序】
    POJ3617 Best Cow Line【贪心】
    洛谷P1583 魔法照片【模拟+排序】
  • 原文地址:https://www.cnblogs.com/jccjcc/p/10785690.html
Copyright © 2020-2023  润新知