简体   繁体   English

如何在 java 中用线分隔二维数组网格?

[英]How to separate by lines a 2d array grid in java?

I'm trying to to create a basic mutiplication table printed out as a grid 2d integer array, basicly I'm having trouble to sepatate the numbers with lines for example "----" or |-|, anyway, below is the basic 2d integer array already showing a multiplication table.我正在尝试创建一个打印为网格 2d integer 数组的基本乘法表,基本上我无法用行分隔数字,例如“----”或 |-|,无论如何,下面是基本的 2d integer 数组已经显示了乘法表。 Please help to separate each number with lines to get view as an excell table or smtg like that.请帮助用行分隔每个数字,以便将其视为 excell 表或类似的 smtg。

package Multip; package 乘法;

public class Multitest {公共 class 多重测试 {

public static void main(String[] args) {

int[][] multiplicationTable = new int[10][10]; int[][] multiplicationTable = new int[10][10];

    for (int row=0; row<10; row++) {
      for (int col=0; col<10; col++) {
          
    multiplicationTable[row][col] = (row+1) * (col+1);
  }
}

    for (int row = 0; row < multiplicationTable.length; row++) {    
        
for (row = 0; row < multiplicationTable.length; row++) {

    for (int col = 0; col < multiplicationTable[row].length; col++) {       
        
       System.out.printf("%4d", multiplicationTable[row][col]);
  }       
    System.out.println();
}
    }
}

} }

You can print a new line with underscore in another for loop after your initial loop which prints the numbers.在打印数字的初始循环之后,您可以在另一个 for 循环中打印一个带下划线的新行。 Then your inner loop might look something like this.那么你的内部循环可能看起来像这样。 The count of underscore would be the space you're giving in between each digit下划线的数量将是您在每个数字之间给出的空间

for (int col = 0; col < multiplicationTable[row].length; col++) {

    System.out.printf(" %4d |", multiplicationTable[row][col]);
}
System.out.println();
for (int col = 0; col < multiplicationTable[row].length; col++) {

    System.out.print("_______");
}
System.out.println();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM