创建一个奇数*奇数的二维数组,使其横竖斜相加都等于一个数
public class Test{public static int size=5;
public static void main(String[] args){
int[][] arr = new int[size][size];
int k =1; www.zzzyk.com
int i = 0;
int j = (int)Math.ceil(size/2);
while(true){
if(i<0){
i=size-1;
}
if(j>size-1){
j=0;
}
arr[i][j] = k;
if(k == size*size)break;
if(k%size == 0){
i+=1;
if(i<0){
i=arr.length-1;
}
if(j>arr.length-1){
j=0;
}
arr[i][j] = ++k;
}
i--;
j++;
k++;
}
for(int m=0;m<arr.length;m++){
for(int n =0; n<arr[m].length;n++){
System.out.print(arr[m][n]+" ");
}
System.out.println();
}
}
}
补充:软件开发 , Java ,