运行错误,救助
//魔方阵打印public class Magic
{
public static final int N=5;
public static void main(String[] args)
{
int m[][]=new int[N][N];
int i,j,number;
i=0;j=N/2;
m[i][j]=1;
for(number=2;number<=N*N;number++){
if(m[(i-1+N)%number][(j+1)%N]==0){
i=(i-1+N)%number;j=(j+1)%N;
}else{
i=(i+1)%N;
}
m[i][j]=number;
}
for(i=0;i<N;i++)
{ for(j=0;j<N;j++)
System.out.print(m[i][j]+"\t");
System.out.println();
}
}
}
通过了编译,但是运行错误,信息如下:
exception in the thread "main" java.lang.ArrayIndexOutOfBoundsException:5
at Magic.java.main<Magic.java:11>