商业网站建设案例课程下载,设计素材模板,做排版的网站,百度一下首页网页手机版题目#xff1a; 第0步#xff1a;输出某个英文文本文件中 26 字母出现的频率#xff0c;由高到低排列#xff0c;并显示字母出现的百分比#xff0c;精确到小数点后面两位。 第1步#xff1a;输出单个文件中的前 N 个最常出现的英语单词。作用#xff1a;一个用于统计文… 题目 第0步输出某个英文文本文件中 26 字母出现的频率由高到低排列并显示字母出现的百分比精确到小数点后面两位。 第1步输出单个文件中的前 N 个最常出现的英语单词。作用一个用于统计文本文件中的英语单词出现频率。 设计思想首先是统计字母我们应该先把要统计的文件读取遍历统计字母出现的次数将大写字母转换为小写字母统计单词也需要将大写字母转换为小写只要遇到空格则记为一个单词遍历一遍统计单词个数。 遇到的问题不知道该如何显示百分比后来在网上查找发现可以用formattedDecimalToPercentage这个函数来实现 package test;import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class zimu {private static String formattedDecimalToPercentage(double decimal){//获取格式化对象NumberFormat nt NumberFormat.getPercentInstance();//设置百分数精确度2即保留两位小数nt.setMinimumFractionDigits(2);return nt.format(decimal);}
public static void main(String []args) {String a1;char aA;int a2[]new int[27];char b1[]new char[26];char b2[]new char[26];double c1[]new double[26];for(int i0;i26;i){b1[i]a;b2[i](char)(a32);a;}try {BufferedReader in new BufferedReader(new FileReader(D:\\piao.txt));String str;while ((str in.readLine()) ! null) {char[] dstr.toCharArray();for(int i0;id.length-1;i) {for(int j0;j26;j) {if(b1[j]d[i]||b2[j]d[i]) {a2[j];}}}}a2[26]0;for(int i0;i26;i) {a2[26]a2[i]a2[26];}for(int i0;i26;i) {c1[i](double)((double)a2[i]/(double)a2[26]);}for(int i0;i26;i) {System.out.print(b1[i]);System.out.print(和);System.out.print(b2[i]);System.out.print(出现的次数为);System.out.print(a2[i]);double d(double)((double)a2[i]/(double)a2[26]);String result2formattedDecimalToPercentage(d);System.out.println( 百分比为result2);}System.out.println( );System.out.println(出现单词次数较多的前十个为);BufferedReader reader new BufferedReader(new FileReader(D:\\\\piao.txt));StringBuffer buffer new StringBuffer();String line null;while ((line reader.readLine()) ! null) {buffer.append(line);}reader.close();Pattern expression Pattern.compile([a-zA-Z]);String string buffer.toString();Matcher matcher expression.matcher(string);//MapString, Integer map new TreeMapString, Integer();String word ;int times 0;while (matcher.find()) {word matcher.group();if (map.containsKey(word)) {times map.get(word);map.put(word, times 1);} else {map.put(word, 1);}}ListMap.EntryString, Integer list new ArrayListMap.EntryString, Integer(map.entrySet());ComparatorMap.EntryString, Integer comparator new ComparatorMap.EntryString, Integer() {public int compare(Map.EntryString, Integer left,Map.EntryString, Integer right) {return (left.getValue()).compareTo(right.getValue());}};Collections.sort(list, comparator);// 排序int last list.size() - 1;int asdad0;for(int ilast;i0;i--) {String key list.get(i).getKey();Integer value list.get(i).getValue();asdadasdadvalue;}for (int i last; i last - 10; i--) {String key list.get(i).getKey();Integer value list.get(i).getValue();System.out.print(key : value);double d(double)((double)value/(double)asdad);String result2formattedDecimalToPercentage(d);System.out.println( 百分比为result2);}} catch (IOException e) {}
}
}结果截图 总结这次课上的作业让我对文件的读取有了进一步的掌握文件的读取已经做过很多次了但是自己还不是很了解应该多加练习formattedDecimalToPercentage这个函数可以用来小数格式化和百分比的显示 转载于:https://www.cnblogs.com/zhang12345/p/11062766.html