/**
 * @author qzw
 */
public class MultiMatrix {
public static void main(String args[]){
    //逆时针输出回形矩阵图
    int[][] arr = getArray(4,4);
    for(int i=0;i<arr.length;i++){
        for(int j=0;j<arr[i].length;j++){
            System.out.print(arr[i][j]+",");
        }
        System.out.println();
    }
}
/**
 * 获取xInt * yInt逆时针回形矩阵数组
 * @param xInt,矩阵x轴大小
 * @param yInt,矩阵y轴大小
 * @return
 */
public static int[][] getArray(int xInt, int yInt){
    int xMax = xInt;
    int yMax = yInt;
    int n = 4;
    int arr[][]=new int[yMax][xMax];
    int x = 0, y = 0;
    int xMin = 0, yMin = 0;
    int size = xMax * yMax;
    boolean flag = true;
    for(int i=0;i<size;i++){
        arr[y][x] = i+1;
        if((y+1)<yMax && flag){
            y++;
        }else if((x+1)<xMax && flag){
            x++;
        }else {
            if(y>yMin){
                y--;
            }else if(x>(xMin+1)){
                x--;
            }else{
                xMax--;
                yMax--;
                xMin++;
                yMin++;
                y++;
                flag = true;
            }
        }
        if((y+1) == yMax && (x+1) == xMax){
            flag = false;
        }
    }
    return arr;
}
}

解决方案 »

  1.   


     arr[y][x] = i+1;//更改为 arr[x][y] = i+1;就能达到你的要求了
      

  2.   

    //再给一次代格式/**
     * @author qzw
     */
    public class MultiMatrix {
    public static void main(String args[]){
        //逆时针输出回形矩阵图
        int[][] arr = getArray(4,4);
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                //System.out.print(arr[i][j]+",");
                System.out.printf("%03d",arr[i][j]);
                System.out.print("\t");
            }
            System.out.println();
        }
    }
    /**
     * 获取xInt * yInt逆时针回形矩阵数组
     * @param xInt,矩阵x轴大小
     * @param yInt,矩阵y轴大小
     * @return
     */
    public static int[][] getArray(int xInt, int yInt){
        int xMax = xInt;
        int yMax = yInt;
        int n = 4;
        int arr[][]=new int[yMax][xMax];
        int x = 0, y = 0;
        int xMin = 0, yMin = 0;
        int size = xMax * yMax;
        boolean flag = true;
        for(int i=0;i<size;i++){
            arr[x][y] = i+1;
            if((y+1)<yMax && flag){
                y++;
            }else if((x+1)<xMax && flag){
                x++;
            }else {
                if(y>yMin){
                    y--;
                }else if(x>(xMin+1)){
                    x--;
                }else{
                    xMax--;
                    yMax--;
                    xMin++;
                    yMin++;
                    y++;
                    flag = true;
                }
            }
            if((y+1) == yMax && (x+1) == xMax){
                flag = false;
            }
        }
        return arr;
    }
    }
      

  3.   

    其实不是很难!首先你要明白题意:
    1、输入n后,创建一个n*n的二维数组,将1到n*n这些数字填如数组。
    2、当然1填入的数组的下标为(0,0)接着下一个数字呐?所以要定义一个参数方向
    具体代码:import java.util.HashSet;
    import java.util.Scanner;
    import java.util.Set;public class Test {
    public static void main(String[] args) throws Exception {
    String line="";
    while(line!=null&&!"q".equalsIgnoreCase(line)){
    System.out.println("请输入大于零的数字(输入q退出)");
    Scanner sc=new Scanner(System.in);
    line=sc.nextLine();
    if(line!=null&&line.matches("\\d++")){
    int n=Integer.parseInt(line);
    int[][] ints=FillNumber(n);
    for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
    System.out.print(ints[i][j]+"\t");
    }
    System.out.println();
    }
    }
    }

    } private static int[][] FillNumber(int n) {
    int[][] ints=new int[n][n];
    int x=0;
    int y=0;
    int direction=0;
    Set<String> used=new HashSet<String>();
    for(int num=1;num<=n*n;num++){
    // System.out.println("("+x+","+y+")="+num);
    ints[x][y]=num;
    direction=nextDirection(x,y,direction,n,used);
    x=nextX(x,direction,n);
    y=nextY(y,direction,n);
    }
    return ints;
    }
    private static int nextY(int y, int direction,int n) {
    if(direction==0){
    return y+1;
    }else if(direction==2){
    return y-1;
    }else{
    return y;
    }
    } private static int nextX(int x, int direction,int n) {
    if(direction==1){
    return x+1;
    }else if(direction==3){
    return x-1;
    }else{
    return x;
    }
    } /**
     * 确定下一个数的方向,0 y+1;1 x+1;2 y-1; 3 x-1 
     * @param x 
     * @param y
     * @param direction 下一个数填充的方向
     * @param n 边界
     * @return
     */
    private static int nextDirection(int x, int y, int direction, int n,Set<String> used) {
    used.add(x+","+y);
    boolean changeDirection=false;
    int x2=nextX(x,direction,n);
    if(x2<0||x2>=n){
    changeDirection=true;
    }
    int y2=nextY(y,direction,n);
    if(y2<0||y2>=n){
    changeDirection=true;
    }
    String xy2=x2+","+y2;
    if(used.contains(xy2)){
    changeDirection=true;
    }
    if(changeDirection){
    return (direction+1)%4;
    }else{
    return direction;
    }
    }}