统计文章字母个数

源代码:

package zifuchuan;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ZiFuChuan {
public static void main(String[] args) throws IOException {
int i=0;
float j=0;
int[] b=new int[58];
float[] c=new float[58];
float e=0;
char f;
int lengTh=0;
BufferedReader reader=new BufferedReader(new FileReader("F:\\piao.log"));
StringBuilder result=new StringBuilder();
String theLine=null;
while(( theLine=reader.readLine())!=null){
result.append(theLine);
}
String a=result.toString();
lengTh=a.length();
for(i=0;i<58;i++) {
b[i]=0;
}
for(i=0;i<lengTh;i++) {
f=a.charAt(i);
if((f<=90&&f>=65)||(f<=122&&f>=97)) {
b[f-65]++;
j++;
}
else continue;
}
for(i=0;i<58;i++) {
c[i]=b[i]/j*100;
}
for(i=0;i<58;i++) {
f=(char)(i+65);
System.out.println(f+" "+b[i]+"个"+" "+String.format("%.2f", c[i])+"%");
}
}
}

运行截图:

 

posted @ 2021-11-12 23:05  听风1234  阅读(40)  评论(0编辑  收藏  举报