尝试在同一行上逐行打印多个2D数组

时间:2019-03-24 17:31:57

标签: java multidimensional-array

我有10个2-D字符串数组,打印时看起来像数字时钟的读数。问题是,我似乎无法弄清楚如何将连续的数组逐行打印出来。我有所有需要代表正确数字的数组,但实际打印效果不佳...

提示:https://imgur.com/f4tr53K

public static void digital(String [][] first, String[][] second, String[][] colon, String[][] third, String[][] fourth)
   {
      for(int i = 0; i < 3; i++)
      {
         for(int j = 0; j < 6; j++)
         {
            System.out.print(first[j][i]);
            System.out.print(second[j][i]);
            System.out.print(colon[j][i]);
            System.out.print(third[j][i]);
            System.out.print(fourth[j][i]);  
         }
         System.out.println();
      }
   } //method to print the clock out

预期:正确的数字时钟读数

实际: 混乱的混乱

  | ||  | |        || ||||
 - --  -        ----  -       
 - --       |   - --       |

2 个答案:

答案 0 :(得分:1)

首先,我建议将数字重写为一种更通用的方法。传递数字数组或数字数组列表。将来也将简化您的生活!

public static void digital(ArrayList<String[][]> arrays) {
    // we don't have anything to print
    if (arrays.size() == 0)
        return;

    // for each column
    for (int i = 0; i < arrays.get(0).length; i++) {

        // for each digit
        for (int j = 0; j < arrays.size(); j++) {
            String[][] arr = arrays.get(j);

            // print current row of a current digit
            for (int k = 0; k < arr[0].length; k++) {
                System.out.print(arr[i][k]);
            }
            // if you need spacing between digits, you can add one more
            // System.out.print(' ');
        }
        System.out.println();
    }
}

这是如何运行此功能的示例:

public static void main(String[] args) {
    ArrayList<String[][]> list = new ArrayList<>();

    list.add(new String[][]{
            {" ", "-", " "},
            {"-", " ", "-"},
            {"-", " ", "-"},
            {"-", " ", "-"},
            {" ", "-", " "},
    });

    list.add(new String[][]{
            {" ", " ", " "},
            {" ", "-", " "},
            {" ", " ", " "},
            {" ", "-", " "},
            {" ", " ", " "},
    });

    list.add(new String[][]{
            {" ", "-", " "},
            {"-", " ", "-"},
            {"-", " ", "-"},
            {"-", " ", "-"},
            {" ", "-", " "},
    });

    digital(list);
}

此功能可缩放至任意数字位数,只需确保所有数字的列数均匹配即可。

答案 1 :(得分:0)

您的循环无处不在。 j应该是垂直的,i应该是水平的,所以让我们从那里开始。

外部循环代表时钟的行。在此您要打印first[j]中的每个元素,然后打印second[j]中的每个元素,依此类推。请注意,打印发生在输出的第一行。不要忘记在它们之间添加空格。第一行结束后,开始新行并重复直到打印每一行。

有许多方法可以减少行数,但是更容易在命令提示符下查看如何处理打印。

public static void digital(String [][] first, String[][] second, String[][] colon, String[][] third, String[][] fourth) {

    for(int j = 0; j < 6; j++) {

        for(in i = 0; i < 3; i++) {
            System.out.print(first[j][i]);
        }
        System.out.print(" ");

        for(in i = 0; i < 3; i++) {
            System.out.print(second[j][i]);
        }
        System.out.print(" ");

        for(in i = 0; i < 3; i++) {
            System.out.print(colon[j][i]);
        }
        System.out.print(" ");

        for(in i = 0; i < 3; i++) {
            System.out.print(third[j][i]);
        }
        System.out.print(" ");

        for(in i = 0; i < 3; i++) {
            System.out.print(fourth[j][i]);
        }
        System.out.println();
    }

} //method to print the clock out