如何格式化 Multi Table 的列间距?

How can I format column spacing for Multi Table?

我目前正在尝试使用 java 制作具有以下输出的多表:

    0  1  2  3  4  5  6  7  8  9
 +------------------------------
0|  0  0  0  0  0  0  0  0  0  0
1|  0  1  2  3  4  5  6  7  8  9
2|  0  2  4  6  8 10 12 14 16 18
3|  0  3  6  9 12 15 18 21 24 27
4|  0  4  8 12 16 20 24 28 32 36
5|  0  5 10 15 20 25 30 35 40 45
6|  0  6 12 18 24 30 36 42 48 54
7|  0  7 14 21 28 35 42 49 56 63
8|  0  8 16 24 32 40 48 56 64 72
9|  0  9 18 27 36 45 54 63 72 81

但是,我得到的不是上面的结果:

   0   1   2   3   4   5   6   7   8   9
 +-------------------------------------------
0| 0  0  0  0  0  0  0  0  0  0
1| 0  1  2  3  4  5  6  7  8  9
2| 0  2  4  6  8  10  12  14  16  18
3| 0  3  6  9  12  15  18  21  24  27
4| 0  4  8  12  16  20  24  28  32  36
5| 0  5  10  15  20  25  30  35  40  45
6| 0  6  12  18  24  30  36  42  48  54
7| 0  7  14  21  28  35  42  49  56  63
8| 0  8  16  24  32  40  48  56  64  72
9| 0  9  18  27  36  45  54  63  72  81

所以,我想知道有什么办法可以解决这个问题吗? 谢谢

这是代码。

import java.util.*;
public class MultiTable 
{
    public static void main(String[] args) 
    {
        int x = 9;
        System.out.print("   ");
        for(int k = 0; k<=x ;k++ ) {
            System.out.print( k + "   ");
        }

       System.out.println("\n +-------------------------------------------");
        for(int i = 0 ;i<=x ;i++) {
             System.out.print(i+ "| ");
            for(int j=0;j<=x ;j++) {
             System.out.print(j*i + "  ");
            }
            System.out.println();
        }
        System.out.println("\n"); 
        System.out.println("\n\n");
    }
}

谢谢。

您可以将 space 添加到一位数,但只有当您的最大数是两位数时才有效。要处理此问题,您应该计算最大数字的长度并使用 spaces.

调整其他数字的长度
for(int j=0;j<=x ;j++) {
    String resStr0 = Integer.valueOf(j*i).toString();
    String resStr = resStr0.length() < 2 ? resStr0+" " : resStr0;
    System.out.print(resStr + "  ");
}

   0   1   2   3   4   5   6   7   8   9   
 +-------------------------------------------
0| 0   0   0   0   0   0   0   0   0   0   
1| 0   1   2   3   4   5   6   7   8   9   
2| 0   2   4   6   8   10  12  14  16  18  
3| 0   3   6   9   12  15  18  21  24  27  
4| 0   4   8   12  16  20  24  28  32  36  
5| 0   5   10  15  20  25  30  35  40  45  
6| 0   6   12  18  24  30  36  42  48  54  
7| 0   7   14  21  28  35  42  49  56  63  
8| 0   8   16  24  32  40  48  56  64  72  
9| 0   9   18  27  36  45  54  63  72  81  

打印格式化字符串,而不是按原样打印。

或者你可以使用 System.out.printf()

将格式化的字符串直接打印到输出

您可以先使用 String.format() 格式化字符串,然后使用 println 打印它。

在您的情况下,您需要强制整数的长度至少为 2 个字符。如果整数位数太少,可以用空格填充。您可以这样做:

int number = 1;

// printf approach
System.out.printf("%3d", number);

// Formatted string approach
String formatted = String.format("%3d", number);
System.out.print(formatted);

格式中可以看到%3d。这意味着:

  • %开始格式化
  • 3 右对齐,3 个空格(负数将左对齐)。我使用 3 这样数字就不会粘在一起了。
  • d 将格式化一个小数(以 10 为基数的整数)

您可以在 Java's official docs

中阅读有关格式化程序的更多信息
  1. 数字控制左侧的 space 填充(与代码相反,代码向右侧添加填充)

  2. 如果是两位数,就补一位space,否则补两位space

    for(int i = 0 ;i<=x ;i++) {
         System.out.print(i+ "|");  // leave padding to the number printing loop
         for(int j = 0 ; j <= x ; j++) {
             int numbertoPrint = j*i;
             int spacePadding = j == 0 || numbertoPrint >= 10 ? 1 : 2;  // one space at the beginning or if two digit number
             System.out.print(" ".repeat(spacePadding) + numbertoPrint);  // repeat() available since java 11
         }
         System.out.println();
    }
    

您可以按如下方式更新代码,以获得所需的输出。更改如下:

1) 第一个 System.out.println 应该有额外的 space.
2) 更改逻辑以在数字之前打印 space,而不是在它之后决定他没有 space。
2) 在内层for循环中加入if条件,决定space个数,根据其后对应数的长度

import java.util.*;
public class MultiTable 
{
    public static void main(String[] args) 
    {
      public static void main(final String[] args) {
    final int x = 9;
    System.out.print("    ");
    for (int k = 0; k <= x; k++) {
      System.out.print(k + "  ");
    }

    System.out.println("\n +------------------------------");
    for (int i = 0; i <= x; i++) {
      System.out.print(i + "|");
      for (int j = 0; j <= x; j++) {
        if (String.valueOf(j * i).length() == 1) {
          System.out.print("  " + j * i);
        } else {
          System.out.print(" " + j * i);
        }
      }
      System.out.println();
    }
    System.out.println("\n");
    System.out.println("\n\n");
  }
}

代码输出:

    0  1  2  3  4  5  6  7  8  9  
 +------------------------------
0|  0  0  0  0  0  0  0  0  0  0
1|  0  1  2  3  4  5  6  7  8  9
2|  0  2  4  6  8 10 12 14 16 18
3|  0  3  6  9 12 15 18 21 24 27
4|  0  4  8 12 16 20 24 28 32 36
5|  0  5 10 15 20 25 30 35 40 45
6|  0  6 12 18 24 30 36 42 48 54
7|  0  7 14 21 28 35 42 49 56 63
8|  0  8 16 24 32 40 48 56 64 72
9|  0  9 18 27 36 45 54 63 72 81