2

この Java ファイルのコンパイルに問題があります。その問題が何であるか理解できません。エクリプス 言う

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at ll.ln.main(ln.java:11)" 

ディレクトリは次のとおりです。

コードは次のとおりです。

package ll;
public class ln {
    public static void main(String arguments[]) {
        String[] pharse={"here i am ","ther you are",
                                 "nobody movewho is in charge here",
                                 "haven dosent far away"};
        for (int count=0;count<=pharse.length;count ++){
            String courenttext=pharse[count];
            char[] chcs=courenttext.toCharArray();
            int[] ln=new int[26];
            for(int i=0;i<=chcs.length;i++){
                if((chcs[i]>'z')||(chcs[i]<'a'))
                 continue;
                ln[chcs[i]-'a'] ++;
            }
            for (int i=0;i<27;i++){
                char t='a';
                t+=i;
                System.out.println(t +": "+ln[i]+"   ");
                }
        }
    }
}
4

4 に答える 4

2

<= これらの各行を次のように置き換え<ます。

for (int count=0;count<=pharse.length;count ++){

次のようにする必要があります。

for (int count=0;count<pharse.length;count ++){

この行にも:

for(int i=0;i<=chcs.length;i++){

する必要があります:

for(int i=0;i<chcs.length;i++){
于 2012-10-19T09:49:29.393 に答える
1

<の代わりに使用<=

for (int i = 0; i < chcs.length; i++) 

完全なコード

        String[] pharse = { "here i am ", "ther you are", "nobody movewho is in charge here",
                "haven dosent far away" };
        for (int count = 0; count < pharse.length; count++) {
            String courenttext = pharse[count];
            char[] chcs = courenttext.toCharArray();
            int[] ln = new int[26];
            for (int i = 0; i < chcs.length; i++) {
                if ((chcs[i] > 'z') || (chcs[i] < 'a'))
                    continue;
                ln[chcs[i] - 'a']++;
            }

            for (int i = 0; i < 26; i++) {
                char t = 'a';
                t += i;
                System.out.println(t + ": " + ln[i] + "   ");

            }

        }
于 2012-10-19T09:50:08.060 に答える
1

変化する

for(int i=0;i<=chcs.length;i++)

for(int i=0;i<chcs.length;i++)

インデックスが長さを超えています

于 2012-10-19T09:51:42.220 に答える
0

3 行を変更します。

  1. for (int count=0;count<=pharse.length;count ++){

    for (int count=0;count<pharse.length;count ++){

  2. for(int i=0;i<=chcs.length;i++){

    for(int i=0;i<chcs.length;i++){

  3. for (int i=0;i<27;i++){

    for (int i=0;i<26;i++){

于 2012-10-19T10:17:49.490 に答える