是要打出一个数字排列图形样子大概是这样的: 
 1   2   3   4  5 
 14 15 16 17 6 
 13 20 19 18 7 
 12 11 10  9  8 
 不只是4行5列,要对任意的都适用,怎么打啊

解决方案 »

  1.   

    class HelixMatrix {    int[][] myMatrix = new int[20][20];
        static int H = 5;     //这个是5列
        static int L = 4;   //这个是4行。这2个可以定义几行几列
        static int count = 0; //填充数字记数
        int hh = H; //矩阵实际大小
        int ll = L;
        int stepX = hh; //初始步距
        int stepY = ll - 1;
        int x = 0; //初始坐标
        int y = 0;    void fillFromLeftToRight(int step) {
            //从左向右填写
            for (int i = 0; i < step; i++) {
                if (myMatrix[y][x] == 0) {
                    myMatrix[y][x] = ++count;
                } else {
                    x++;
                    myMatrix[y][x] = ++count;
                }
            }
        }    void fillFromUpToDown(int step) {
            //从上向下填写
            for (int i = 0; i < step; i++) {
                y++;
                myMatrix[y][x] = ++count;
            }
        }    void fillFromRightToLeft(int step) {
            //从右向左填写
            for (int i = 0; i < step; i++) {
                x--;
                myMatrix[y][x] = ++count;
            }
        }    void fillFromDownToUp(int step) {
            //从下向上填写
            for (int i = 0; i < step; i++) {
                y--;
                myMatrix[y][x] = ++count;
            }
        }    public void make() {
            //生成矩阵:从左到右、从上到下、从右到左、
            do {
                //从下到上,每一循环填一圈,很好理解吧。:)
                if (count != H * L) {
                    fillFromLeftToRight(stepX--);
                }
                if (count != H * L) {
                    fillFromUpToDown(stepY--);
                }
                if (count != H * L) {
                    fillFromRightToLeft(stepX--);
                }
                if (count != H * L) {
                    fillFromDownToUp(stepY--);
                }
            } while (count != H * L);
        }    public void print() {
            //打印出来,要注意排版哟!J if语句控制补空格
            for (int i = 0; i < L; i++) {
                for (int j = 0; j < H; j++) {
                    if (myMatrix[i][j] < 10) {
                        System.out.print("   " + myMatrix[i][j]);
                    } else if (myMatrix[i][j] >= 100) {
                        System.out.print(" " + myMatrix[i][j]);
                    } else {
                        System.out.print("  " + myMatrix[i][j]);
                    }
                }
                System.out.println();
            }
        }
    }
    public class PrintHelixMatrix {    public static void main(String[] args) {
            HelixMatrix aa = new HelixMatrix();
            aa.make();
            aa.print();
        }
    }