java求字母个数

来源:百度知道 编辑:UC知道 时间:2024/09/18 04:14:12
这个是我们老师的要求:
写一个程序:输入一句英文,输出字母个数,单词个数,最长的单词个数,和一个26个字母出现频率的表格。必须用数组的方法来实现26个字母出现的频率。(输入的英文只有空格没有标点)
效果是这样的:
输入一句英文:
Now is the time for all good men to come to the aid of their country

这句话有 53 个字母.
这句话有 16 个单词.
最长的一个单词有 7 个字母.
每个字母出现的频率是
A -- 2
C -- 2
D -- 2
E -- 6
F -- 2
G -- 1
H -- 3
I -- 4
L -- 2
M -- 3
N -- 2
O -- 9
R -- 3
S -- 1
T -- 7
U -- 1
W -- 1
Y -- 1

之前几个要求都还好,可是我实在不知道要怎么做到实现每个字母出现的频率。而且要是用数组的方法。是不是把26个字母存到一个数组里还是怎么样啊!谢谢指教!!

import java.io.IOException;
public class LetterCounter{
public static void main(String[] args){
byte[] buffer=new byte[256];
int letterCount=0,wordCount=0,maxLength=0,charCount=0;
int[] letterFeq=new int[26];
try{
charCount=System.in.read(buffer)-2;
}
catch(IOException ex){}
char[] charArray=new String(buffer,0,charCount).concat(" ").toUpperCase().toCharArray();
for(int i=0;i<=charCount;){
if(charArray[i]==' '){
i++;
continue;
}
wordCount++;
int end=i,len=0;
while(charArray[end]!=' '){
letterFeq[charArray[end]-'A']++;
letterCount++;
end++;
}
if(maxLength<end-i){
maxLength=end-i;
}
i=end;
}
System.out.format("共有%1$d个字母,%2$d个单词,最长单词%3$d个字母\n",letterCount,wordCount,maxLength);
for(int i=0;i<26;i++){