在如何计算百分比方面遇到了一些麻烦(缺乏理解)。我需要找出:
Q1。如何获得每个字母频率的百分比(满分 100)?,也是
Q2。如何获取段落中每个第一个单词的字母频率?
到目前为止,这是我的代码:
import java.io.File;
import java.util.*;
import java.io.*;
public class LetterFrequency
{
public static void main(String[] args )
{
char[] capital = { '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'};
char[] small = { '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' };
Scanner scan;
try {
scan = new Scanner(new File("F:/programming principles/Programming Principles - PART B/enciphered.txt"));
} catch (Exception e) {
System.out.println("File not found");
return;
}
int[] count = new int[26];
while(scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println("Line read: " + line);
char[] digit = line.toCharArray();
for(int i = 0; i < digit.length; i++) {
for(int j = 0; j < 26; j++) {
if(digit[i] == capital[j] || digit[i] == small[j]) {
count[j]++;
break;
}
}
}
}
System.out.println("");
System.out.println("Comlete count");
for (int i = 0; i < 26; i++)
{
System.out.print(" " + small[i]);
System.out.println(" " + count[i]);
//calculate percentage for the full count
}
}
}