数组表错误

时间:2014-05-27 20:34:26

标签: java arrays

在第133行,我不断收到错误ArrayIndexOutOfBoundsException,我无法找到它的解决方案。我尝试改变一些价值但无济于事任何想法?我觉得我可能需要对列数和行数做一些事情,但我不是百分百肯定。非常感谢提前!

import java.io.*;
class myTable
{
public static void main (String [] args) throws IOException
{

int Input, Input2, Input3, Input4, Input5, Input6, Input7, Input8, Input9, Input10;//Inputs are int
int Input11, Input12, Input13, Input14, Input15, Input16, Input17, Input18, Input19, Input20;//Inputs are int
    InputStreamReader inStream = new InputStreamReader (System.in);
BufferedReader mVHS = new BufferedReader (inStream);
System.out.println("Enter 20 numbers to make a table");

System.out.println("Type a number:");//Enter a number
String userInput = mVHS.readLine();
Input = Integer.parseInt(userInput);

System.out.println("Type a number:");//Enter a number
String userInput2 = mVHS.readLine();
Input2 = Integer.parseInt(userInput2);

System.out.println("Type a number:");//Enter a number
String userInput3 = mVHS.readLine();
Input3 = Integer.parseInt(userInput3);

System.out.println("Type a number:");//Enter a number
String userInput4 = mVHS.readLine();
Input4 = Integer.parseInt(userInput4);;

System.out.println("Type a number:");//Enter a number
String userInput5 = mVHS.readLine();
Input5 = Integer.parseInt(userInput5);

System.out.println("Type a number:");//Enter a number
String userInput6 = mVHS.readLine();
Input6 = Integer.parseInt(userInput6);

System.out.println("Type a number:");//Enter a number
String userInput7 = mVHS.readLine();
Input7 = Integer.parseInt(userInput7);

System.out.println("Type a number:");//Enter a number
String userInput8 = mVHS.readLine();
Input8 = Integer.parseInt(userInput8);

System.out.println("Type a number:");//Enter a number
String userInput9 = mVHS.readLine();
Input9 = Integer.parseInt(userInput9);

System.out.println("Type a number:");//Enter a number
String userInput10 = mVHS.readLine();
Input10 = Integer.parseInt(userInput10);

System.out.println("Type a number:");//Enter a number
String userInput11 = mVHS.readLine();
Input11 = Integer.parseInt(userInput11);

System.out.println("Type a number:");//Enter a number
String userInput12 = mVHS.readLine();
Input12 = Integer.parseInt(userInput12);

System.out.println("Type a number:");//Enter a number
String userInput13 = mVHS.readLine();
Input13 = Integer.parseInt(userInput13);

System.out.println("Type a number:");//Enter a number
String userInput14 = mVHS.readLine();
Input14 = Integer.parseInt(userInput14);;

System.out.println("Type a number:");//Enter a number
String userInput15 = mVHS.readLine();
Input15 = Integer.parseInt(userInput15);

System.out.println("Type a number:");//Enter a number
String userInput16 = mVHS.readLine();
Input16 = Integer.parseInt(userInput16);

System.out.println("Type a number:");//Enter a number
String userInput17 = mVHS.readLine();
Input17 = Integer.parseInt(userInput17);

System.out.println("Type a number:");//Enter a number
String userInput18 = mVHS.readLine();
Input18 = Integer.parseInt(userInput18);

System.out.println("Type a number:");//Enter a number
String userInput19 = mVHS.readLine();
Input19 = Integer.parseInt(userInput19);

System.out.println("Type a number:");//Enter a number
String userInput20 = mVHS.readLine();
Input20 = Integer.parseInt(userInput20); 

int[][] myTable = new int[5][4];
myTable [0][0] = Input;
myTable [0][1] = Input2;
myTable [0][2] = Input3;
myTable [0][3] = Input4;
myTable [1][0] = Input5;
myTable [1][1] = Input6;
myTable [1][2] = Input7;
myTable [1][3] = Input8;
myTable [2][0] = Input9;
myTable [2][1] = Input10; 
myTable [2][2] = Input11;
myTable [2][3] = Input12;
myTable [3][0] = Input13;
myTable [3][1] = Input14;
myTable [3][2] = Input15; 
myTable [3][3] = Input16;
myTable [4][0] = Input17;
myTable [4][1] = Input18;
myTable [4][2] = Input19;
myTable [4][3] = Input20; 

for(int row = 0; row < myTable.length; row++)
    {
        System.out.print("Row " + row + ": ");
        for (int column = 0; column < myTable[row].length; column++)
            System.out.print(myTable[row][column] + " ");

        System.out.println( );
    }
  System.out.println("Average of column 1:" + (myTable [0][0] + myTable [0][1] + myTable [0][2] + myTable [0][3] + myTable [0][4]) / 4  );
  System.out.println("Average of column 2:" + (myTable [1][0] + myTable [1][1] + myTable [1][2] + myTable [1][3] + myTable [1][4]) / 4  );
  System.out.println("Average of column 3:" + (myTable [2][0] + myTable [2][1] + myTable [2][2] + myTable [2][3] + myTable [2][4]) / 4  );
  System.out.println("Average of column 4:" + (myTable [3][0] + myTable [3][1] + myTable [3][2] + myTable [3][3] + myTable [3][4]) / 4  );

}
}

2 个答案:

答案 0 :(得分:5)

问题在于:

System.out.println("Average of column 1:" + (myTable [0][0] + myTable [0][1] + myTable [0][2] + myTable [0][3] + myTable [0][4]) / 4  );

myTable[0][4]定义为myTable时,您正在访问int[5][4]。修复此问题,您的代码就可以运行。

System.out.println("Average of column 1:" + (myTable [0][0] + myTable [0][1] + myTable [0][2] + myTable [0][3]) / 4  );

这里最大的问题是你的设计。您应该使用for循环来设置myTable中的值,而不是使用20个变量(!)。这是一个例子:

int[][] myTable = new int[5][4];
for (int i = 0; i < myTable.length; i++) {
    for (int j = 0; j < myTable[i].length; j++) {
        System.out.println("Type a number:");
        myTable[i][j] = Integer.parseInt(mVHS.readLine());
    }
}

答案 1 :(得分:0)

基本上,myTable是针对最高[4][3]的值定义的,这很好,但在输出语句中,您会得到混合,就像它们达到[3][4]一样。您需要5行而不是4行,并且需要删除每行中的+ myTable [?][4]