Java 确定每个单词每个字母的平均出现率

Java 确定每个单词每个字母的平均出现率,java,arrays,Java,Arrays,我正在制作一个程序,用数组从一系列单词中确定每个字母的出现频率。它将输出每个单词每个字母的平均出现次数,这就是我的问题 Input: The quick brown fox jumps over{enter} the lazy dog{enter} 1{enter} //1 terminates the program and shows the output Output: a: 0.11 b: 0.11 c: 0.11 d: 0.11 e: 0.33 f: 0.11 g: 0.1

我正在制作一个程序,用数组从一系列单词中确定每个字母的出现频率。它将输出每个单词每个字母的平均出现次数,这就是我的问题

Input:
The quick brown fox jumps over{enter}
the lazy dog{enter}
1{enter} //1 terminates the program and shows the output

Output:
a: 0.11  b: 0.11  c: 0.11  d: 0.11  e: 0.33
f: 0.11  g: 0.11  h: 0.22  i: 0.11  j: 0.11 
k: 0.11  l: 0.11  m: 0.11  n: 0.11  o: 0.44
p: 0.11  q: 0.11  r: 0.22  s: 0.11  t: 0.22
u: 0.22  v: 0.11  w: 0.11  x: 0.11  y: 0.11
z: 0.11
这是我目前的代码:

public static void main( String[] args ) 
        {
            Scanner scn=new Scanner(System.in);
            int y=0;
            int[] alArray = new int[26];
            while(y==0)
            {
                String in = scn.next().toLowerCase();
                for (int x=0; x<in.length(); x++) 
                {
                     char chr=in.charAt(x);
                     int val=(int) chr;
                     if (val>=97 && val<=122)
                     {
                         alArray[chr-'a']++;
                     }
                }
                for (int x=0; x<alArray.length; x++) 
                {
                    if(alArray[x]>0)
                    {
                        char chr = (char) (x+97);
                        System.out.println(chr+": "+alArray[x]);

                    }         
                }
            }
        }

如果你想知道每个字母出现的平均次数,那么你需要对它们进行平均,而不仅仅是计数。i、 e.计算发生次数后:-

for (int x=0; x<in.length(); x++) { ... }

for(int x=0;x第一个示例不清楚:输入中没有
h
,但输出显示
h:0.22
…您的问题中没有问题。@rossdew再次检查?@user3152606您再次检查“这是我的问题”这不是一个问题。所以,问题似乎是你只做了一点计数,然后它会逐字计数字母,而不是整个短语。是吗?你不应该做
actualOccurences/possibleOccurences
for (int x=0; x<in.length(); x++) { ... }
for (int x=0; x<alArray.length; x++) {
{ 
   if(alArray[x]>0)//avoid division by zero
   {
     //for each letter, do (actualOccurences / possibleOccurences )
     alArray[x] = alArray[x] / in.length;
   }
}