我正在尝试查找文档中每个字母的频率,因此该程序会计算每个字母的出现次数,然后查找字母总数。然后它应该找到频率,问题是它正确计算字母和总数,但百分比不正确。我该如何计算百分比?
//find number of occurrences for each letter
String[] letter = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int[] count = new int[26];
int totalCount = 0;
for(int i = 0; i < subset.length(); i++)
{
char letters = subset.charAt(i);
if(letters == 'a')
count[0]++;
else if(letters == 'b')
count[1]++;
else if(letters == 'c')
count[2]++;
else if(letters == 'd')
count[3]++;
else if(letters == 'e')
count[4]++;
else if(letters == 'f')
count[5]++;
else if(letters == 'g')
count[6]++;
else if(letters == 'h')
count[7]++;
else if(letters == 'i')
count[8]++;
else if(letters == 'j')
count[9]++;
else if(letters == 'k')
count[10]++;
else if(letters == 'l')
count[11]++;
else if(letters == 'm')
count[12]++;
else if(letters == 'n')
count[13]++;
else if(letters == 'o')
count[14]++;
else if(letters == 'p')
count[15]++;
else if(letters == 'q')
count[16]++;
else if(letters == 'r')
count[17]++;
else if(letters == 's')
count[18]++;
else if(letters == 't')
count[19]++;
else if(letters == 'u')
count[20]++;
else if(letters == 'v')
count[21]++;
else if(letters == 'w')
count[22]++;
else if(letters == 'x')
count[23]++;
else if(letters == 'y')
count[24]++;
else if(letters == 'z')
count[25]++;
}
//find total characters in document
double[] frequency = new double[26];
for(int i = 0; i < 26; i++)
{
totalCount += count[i];
frequency[i] = ((double)count[i] / (totalCount + 1)) * 100; //the problem
}
System.out.println(totalCount);
//for(int i = 0; i < 26; i++)
//{
// frequency[i] = (double)(count[i] / totalCount * 100);
//}
//print
System.out.println(" Letter Occurrences Frequency");
System.out.println("--------------------------------");
for(int i = 0; i < 26; i++)
{
System.out.println(" " + letter[i] + "\t " + count[i] + "\t " + df.format(frequency[i]));
}
答案 0 :(得分:1)
在拥有totalCount之后需要计算频率:
for(int i = 0; i < 26; i++) {
totalCount += count[i];
}
System.out.println(totalCount);
System.out.println(" Letter Occurrences Frequency");
System.out.println("--------------------------------");
for(int i = 0; i < 26; i++) {
frequency[i] = ((double)count[i] / (totalCount + 1)) * 100;
System.out.println(" " + letter[i] + "\t " + count[i] + "\t " + df.format(frequency[i]));
}
旁注
你不需要forC循环for totalCount,因为它只是字符串的长度(即totalCount = subset.length()
)
你不需要25个if else语句来查找字母数量......只需:
count[subset.charAt(i) - 61]++;
61是字母'a'