我有此代码,但我不断收到此错误消息,我不知道为什么?
线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:2 在 javaapplication28.JavaApplication28.main(JavaApplication28.java:38) 1 2 Java 结果:1
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("This program transposes a matrix.");
System.out.println("Please enter the number of rows");
int rows = input.nextInt();
System.out.println("User enters: "+rows);
System.out.println("Please enter the number of columns");
int columns = input.nextInt();
System.out.println("User enters: "+columns);
int [][]matrix=new int[rows][columns];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[i].length;j++){
System.out.print("Enter value for row [" +i+ "] column [" +j+"]:");
matrix[i][j]=input.nextInt();
}
}
for(int i=0;i<=matrix.length;i++){
System.out.println();
for(int j=0;j<=matrix.length;j++){
System.out.print(matrix[i][j]+" ");
}
}
System.out.println("The transpose of this matrix has" +columns+"rows and"+rows+"columns and the transpose is:");
for(int i=0;i<=matrix.length;i++){
System.out.println();
for(int j=0;j<=matrix.length;j++){
System.out.print(matrix[j][i]+" ");
}
}}
}