以Matrix格式打印出二维数组 [英] printing out a 2-D array in Matrix format

查看:102
本文介绍了以Matrix格式打印出二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以矩阵框格式打印出一个简单的int [] [],就像我们手写矩阵的格式一样。简单的循环运行显然不起作用。如果它有助于我尝试在linux ssh终端中编译此代码。

How can I print out a simple int [][] in the matrix box format like the format in which we handwrite matrices in. A simple run of loops doesn't apparently work. If it helps i'm trying to compile this code in a linux ssh terminal.

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        System.out.println(matrix[i][j] + " ");
    }
    System.out.println();
}


推荐答案

final int[][] matrix = {
  { 1, 2, 3 },
  { 4, 5, 6 },
  { 7, 8, 9 }
};

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

产生:

1 2 3
4 5 6
7 8 9

这篇关于以Matrix格式打印出二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆