Java回形数

import java.util.Scanner;

/**`在这里插入代码片`
 * @author DDman
 * @create 2020-08-19-11:28
 */
public class LoopNumber {
    public static void main(String[] args) {
        //获取二维数组大小row,column
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入数组的行列,用空格隔开:");
        int row = scanner.nextInt();
        int column = scanner.nextInt();

        //获取对应二维数组
        int arr[][] = new int[row][column];

        int num = 1;    //起始值
        arr[0][0] = num;
        int max = (row)*(column);   //最大值
        int upBorder = -1;        //上界
        int downBorder = row;     //下界
        int leftBorder = -1;      //左界
        int rightBorder = column; //右界
        int x = 0; //行坐标,当前位置
        int y = 0; //列坐标,当前位置

        //给数组元素赋值
        while(num < max){

            //向右
            //当上临边界 且 y + 1小于右边界,向右
            if(x - 1 == upBorder && y + 1 < rightBorder){
                //赋值
                y++;
                num++;
                arr[x][y] = num;
                //当向右后,y + 1 == 右界时,上界+1
                if(y + 1 == rightBorder){
                    upBorder++;
                }
            }

            //向下
            //当右临边界 && x+1小于下边界,向下
            if(y + 1 == rightBorder && x + 1 < downBorder){
                //赋值
                x++;
                num++;
                arr[x][y] = num;
                //当向下后,x + 1 == 下界时,右界-1
                if(x + 1 == downBorder){
                    rightBorder--;
                }
            }

            //向左
            //当下临边界 && y-1大于左边界,向左
            if(x + 1 == downBorder && y - 1 > leftBorder){
                //赋值
                y--;
                num++;
                arr[x][y] = num;
                //当向左后,y - 1 == 左界时,下界-1
                if(y - 1 == leftBorder){
                    downBorder--;
                }
            }

            //向上
            //当左临边界 && x-1大于上边界,向上
            if(y - 1 == leftBorder && x - 1 > upBorder){
                //赋值
                x--;
                num++;
                arr[x][y] = num;
                //当向上后,x - 1 == 上界时,左界+1
                if(x - 1 == upBorder){
                    leftBorder++;
                }
            }

        }


        //输出数组
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[0].length; j++){
                System.out.printf("%3d ", arr[i][j]);
            }
            System.out.println();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值