每日日报2020 11/3

昨天上课上了文件与流的相关知识,把完整的代码敲完了,有点开心。

package projrct1;

import java.io.*;
import java.util.Arrays;
public class shouji {
public static void main(String[] args) throws IOException {

//文件读取
FileReader fr=new FileReader("E:/新建文件夹/《飘》英文版.txt");
BufferedReader bufr=new BufferedReader(fr);
StringBuffer str=new StringBuffer();
String Line=null;
while((Line=bufr.readLine())!=null) {
str.append(Line);
}
bufr.close();

//控制大小写字母的输出以及他们的数目

double capitalletter[]=new double[26];
double lowercaseletter[]=new double[26];
int count=0;
/*
for(int i=0;i<str.length();i++) {
char ch=str.charAt(i);
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') {
for(int j=0;j<26;j++) {
if(ch=='A'+j)
capitalletter[j]++;
}
for(int k=0;k<26;k++) {
if(ch=='a'+k)
lowercaseletter[k]++;
}
count++;
}
}
*/

for(int i=0;i<str.length();i++) {
char ch=str.charAt(i);
if(ch==32) {
count++;
}
}

//计算百分比
double percentage1[]=new double[52];
double percentage2[]=new double[52];
for(int i=0;i<26;i++) {
percentage1[i]=capitalletter[i]/count;
percentage2[i]=percentage1[i];
}
for(int i=26;i<52;i++) {
percentage1[i]=lowercaseletter[i-26]/count;
percentage2[i]=percentage1[i];
}

//排序
Arrays.sort(percentage1);


for(int i=51;i>=0;i--) {

int max=0;
for(int j=0;j<52;j++) {
if(percentage2[j]==percentage1[i])
max=j;
}
if(max>=26)
System.out.print(((char)('a'+max-26))+":");
else
System.out.print(((char)('A'+max))+":");
System.out.println(String.format("%.2f",percentage1[i]*100)+'%');
}
System.out.println("英文字母总数为:"+count);
}

}

posted @ 2020-11-03 22:18  宋振兴  阅读(42)  评论(0编辑  收藏  举报