美文网首页
打印表格

打印表格

作者: 蓝笔头 | 来源:发表于2021-09-13 12:59 被阅读0次
public class GridPrinter {

    public static void main(String[] args) {
        print(5, 5);
    }

    public static void print(int rows, int columns) {
        for (int i = 1; i < rows; ++ i) {
            printLine('+', '-', columns);
            printLine('|', ' ', columns);
        }
        printLine('+', '-', columns);
    }

    private static void printLine(char delimiter, char padding, int columns) {
        for (int j = 0; j < columns; ++ j) {
            printCell(delimiter, padding);
        }
        System.out.println(delimiter);
    }

    public static void printCell(char delimiter, char padding) {
        // 可以调整表格单元的宽度
        int paddingWidth = 4;
        System.out.print(delimiter);
        for (int j = 0; j < paddingWidth; ++ j) {
            System.out.print(padding);
        }
    }
}

控制台输出:

+----+----+----+----+----+
|    |    |    |    |    |
+----+----+----+----+----+
|    |    |    |    |    |
+----+----+----+----+----+
|    |    |    |    |    |
+----+----+----+----+----+
|    |    |    |    |    |
+----+----+----+----+----+

相关文章

网友评论

      本文标题:打印表格

      本文链接:https://www.haomeiwen.com/subject/lysvwltx.html