-1

我试图为每个字符(A 到 Z 和 a 到 z)编写一个分配计数的方法。到目前为止我所拥有的是

public int[] getLetterDistribution() {
    int[] distribution = new int[26];               
    return distribution;
}

我目前的尝试是:

public int[] getLetterDistribution() {
    int[] distribution = new int[26];

    for (int count = 0; count < distribution.length; count++) {
        String current = distribution[count];

        char[] letters = current.toCharArray();
        for (int count2 = 0; count2 < letters.length; count2++) { 
            char lett = letters[count2]; 
            if ( (lett >= 'A') & (lett <= 'Z') ) {
                    letterCount[lett - 'A']++;
            }
        }
    }
    for (char count = 'a'; count <= 'z'; count++) {
        System.out.print(count + ": " +
        letterCount[count - 'a'] +" ");
    }
    System.out.println();

    return distribution;
}

但我不断收到错误。对于那些java高手来说,一个彻底的解释会很棒。有谁知道我在这里做错了什么?

4

3 回答 3

3

您的代码中有三个编译错误。一个在这里:

int[] distribution = new int[26];
String current = distribution[count];

由于分布是一个整数数组,因此您无法从中获取字符串。

这里还有另外两个错误:

letterCount[lett - 'A']++;
letterCount[count - 'a'] +" ");

数组letterCount从未声明过,因此它实际上不存在。

于 2013-04-04T17:17:13.183 回答
1

Your biggest problem seems to be that you don't have a source for your strings that you're going to count the distribution of. Right now you're trying to get Strings from an array of ints.

String current = distribution[count];

You need to get the strings from somewhere else

于 2013-04-04T17:13:36.903 回答
0

如果您的发行版区分大小写,那么您将需要一个 int[52] 而不是 int[26]

于 2013-04-04T17:12:25.123 回答