大家好,Java 新手,进展缓慢。在添加行和列之前,我试图用随机数填充二维数组。到目前为止,我可以创建数组并显示它,并且我很确定我已经对添加位进行了排序。但是当我尝试用随机数填充它时,我得到了一个 outofboundsexception。我哪里错了?
public static void main(String[] args)
{
//create the grid
final int row = 9;
final int col = 9;
int [][] grid = new int [row][col];
//fill the grid
for (int i=0; i<grid.length; i++)
grid[i][grid[i].length] = (int)(Math.random()*10);
//display output
for(int i=0;i<grid.length; i++)
{
for(int j=0; j<grid[i].length; j++)
System.out.print(grid[i][j]+"");
System.out.println();
}
int sum = 0;
for (int i = 0; i < grid.length; i++) {
System.out.println("This row sums up to: " + sum);
for (int j = 0; j < grid[i].length; j++) {
sum += grid[j][i];
}
System.out.println("This column sums up to: " + sum);
}
}