解决此ArrayOutOfBoundException

时间:2014-05-21 18:46:00

标签: java

import java.util.Scanner; public class Askisi1 {

public static void main(String[] args) {
    Scanner ch=new Scanner(System.in);
    Scanner in = new Scanner(System.in);
    String[] onomata= new String[6];
    int[] pontoi1=new int[10];
    int[] pontoi2=new int[10];
    int[][] pontoi=new int [6][10];

    int i,k;


        for (i=0; i<onomata.length; i++)
        {
            System.out.println("Parakalw doste to onoma tou"+" " +(i+1)+"ou"+" "+"ipopsifiou!");
            onomata[i] =  ch.nextLine();

        }

        for (i=0; i<=9; i++)
        {
            for (k=0; k<=5; k++)
            {
                System.out.println("Parakalw doste tous pontous tou"+" " +(i+1)+"ou"+" "+"ipopsifiou ston"+" " +(k+1)+"o"+" "+"agwna!!");
                pontoi[i][k]=in.nextInt();
            }
        }

        pontoi1=total1(pontoi);
        pontoi2=total2(pontoi);

        for (i=0; i<=9; i++)
        {
            System.out.println("Oi pontoi tou"+" "+i+1+"ipopsifiou einai"+pontoi1[i]);
        }

        for (i=0; i<=9; i++)
        {
            System.out.println("Oi pontoi tis"+" "+i+1+" "+"s"+"ipopsifiou einai"+pontoi2[i]);
        }

}

        public static int[] total1 (int[][] array) {


                int total = 0;
                int z=0; int j=0;
                int[] a=new int[10];
                for (z=0; z<=5; z++)
                {
                    for (j=0; j<=9; j++)
                    {
                        total=total+array[z][j];
                    }
                    a[z]=total; 
                }

                return a;
            }


    public static int[] total2 (int[][] array) {
                int total2 = 0; int total3=0;
                int z=0; int j=0;
                int[] b=new int[10];
                for (z=0; z<=5; z++)
                {
                    for (j=0; j<=9; j++)
                    {
                        total2=total2+array[z][j];
                    }
                    total3=total3+total2+array[z][j];
                    b[z]=total3;
                }


                return b;
            }

}

大家好!

由于编译很好而没有发现任何错误,在第二个循环中,程序自行停止说:&#34;线程中的异常&#34; main&#34; java.lang.ArayIndexOutOfBoundsException:6在Askisi.main

很抱歉代码,但大多数变量和文字都是在greeklish !! 在此先感谢您的帮助! :)

2 个答案:

答案 0 :(得分:3)

在嵌套的for循环中,索引是向后的。您已将pontoi声明为int[][] pontoi=new int [6][10];,因此第一个索引的范围从05,第二个索引的范围从09 。变化

pontoi[i][k]=in.nextInt();

pontoi[k][i]=in.nextInt();

答案 1 :(得分:1)

int[][] pontoi=new int [6][10]; 并在你的嵌套循环中

for (i=0; i<=9; i++) { for (k=0; k<=5; k++) { System.out.println("Parakalw doste tous pontous tou"+" " +(i+1)+"ou"+" "+"ipopsifiou ston"+" " +(k+1)+"o"+" "+"agwna!!"); pontoi[i][k]=in.nextInt(); } }  // i = 0到9和k = 0到5但是在最大值你可以操纵pontoi [5] [9],你在pontoi [i = 6] [k],

获得约束异常