A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘德坤 中级黑马   /  2015-10-21 21:27  /  165 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima;

import java.util.Scanner;

/*9、 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。 如: n = 4 则打印:
1    2    3    4
12   13   14   5
11   16   15   6
10   9    8    7
* */
public class Test9 {
        public static void main(String[] args){
            //从键盘输入大于0的整数数得到螺旋排列的二维数组
                print(8);
        }
//判断从键盘输入的值的合法性并获取该值
//        public static int getNum(){
//                boolean judge = true; //定义Boolean类型的值来判断是否跳出循环
//                int result = 0;//定义键盘获取的初值
//                while(judge){
//                Scanner scan = new Scanner(System.in);
//                System.out.println("输入一个大于0的整数:");
//                try{
//                        result = Integer.parseInt(scan.nextLine());       
//                        scan.close();
//                    if(result<0){
//                            throw new Exception();//不符合则抛出异常
//                    }
//                    judge = false;
//                 }catch(Exception e){
//                         System.out.println("输入值非法,请重新输入");
//                    }
//                }
//                return result;
//        }
        //得到键盘输入的数值并打印二维数组
        public static void print(int num){
                //定义等长的二维数组
                int[][] arr = new int[num][num];
                int n = arr.length;  
        int count = 0;  
        int max = 0;
        recArr(arr,n,count,max);  
        printArr(arr);          //执行打印  
        }
        public static void recArr(int[][] arr,int n,int count,int max){  
        //递归控制条件  
        if(n>0){  
            //纵坐标控制值  
            int k = 0;  
            //(n-1)*4代表每一圈的数值范围  
            for(int i=0;i<(n-1)*4;i++){  
                //在上边赋值  
                if(i<n-1){  
                    arr[count+0][count+i] = ++max;  
                }  
                //向右边赋值  
                else if(i<2*n-2){  
                    arr[count+k++][arr.length-1-count]=++max;  
                }  
                //在下边赋值  
                else if(i<3*n-3){  
                    arr[arr.length-1-count][(k--)+count]=++max;  
                }  
                //向左边赋值  
                else if(i<4*n-4){  
                    arr[arr.length-1-(k++)-count][0+count]=++max;     
                }  
            }  
            //当n为奇数时,存在n=1的情况,最里圈只有一个数  
            if(n==1){  
                arr[arr.length/2][arr.length/2]=max+1;  
            }  
            //增加圈数  
            count++;  
            //边界每次减少两个数值  
            n -= 2;  
            //递归  
            recArr(arr,n,count,max);  
        }  
    }  
    //打印二维数组  
    public static void printArr(int[][] arr){  
        //二维数组需要双重循环打印  
        for(int[] ar : arr){  
            for(int a : ar){  
                if(a<10)  
                    System.out.print(" "+a+" ");  
                else  
                    System.out.print(a+" ");  
            }  
            System.out.println();  
        }  
    }  
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马