码迷,mamicode.com
首页 > 编程语言 > 详细

回行打印二维数组

时间:2020-04-24 01:22:09      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:style   sys   and   rgs   color   dom   ann   col   oid   

回行打印二维数组

public class Test {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        int[][] arr = new int[n][m];
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = random.nextInt(10);
            }
        }

        for (int i = 0; i < n; i++) {
            System.out.print("[");
            for (int j = 0; j < m; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.print("]");
            System.out.println();
        }
        System.out.println();

        int r_begin = 0, c_begin = 0;
        while (r_begin <= n || c_begin <= m) {
            for (int i = r_begin, j = c_begin; j < m && r_begin < n; j++)
                System.out.print(arr[i][j] + " ");
            r_begin++;
            for (int i = r_begin, j = m - 1; i < n && c_begin < m; i++)
                System.out.print(arr[i][j] + " ");
            m--;
            for (int i = n - 1, j = m - 1; j >= c_begin && r_begin < n; j--)
                System.out.print(arr[i][j] + " ");
            n--;
            for (int i = n - 1, j = c_begin; i >= r_begin && c_begin < m; i--)
                System.out.print(arr[i][j] + " ");
            c_begin++;
        }

    }
}

 

回行打印二维数组

标签:style   sys   and   rgs   color   dom   ann   col   oid   

原文地址:https://www.cnblogs.com/zhihaospace/p/12764857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!