提问者:小点点

如何在控制台中使用ASCII创建表?


我想这样组织信息:

信息用单元格组织,而用System. out.println信息会非常杂乱无章。


共3个答案

匿名用户

您可以使用System.out.format()或System.out.printf()(printf内部只是调用格式,因此两种方法给出相同的结果)。

您将在下面找到示例,该示例将文本左对齐并用空格填充未使用的位置。字符串向左对齐可以使用 %-15s 实现,这意味着:

    < li>%保留(占位符) < li>15字符的“位置” 字符串数据类型的< li>s < li>-并从左侧开始打印。

如果要处理数字,请使用< code>d后缀,如< code>%-4d来保留至少4个“空格”,数字将从“列”左侧打印。

BTW<code>printf</code>不会在打印数据后自动添加行分隔符,所以如果我们想将光标移动到下一行,我们需要自己做。我们可以使用\r\n,或者让Formatter生成与操作系统相关的行分隔符(类似于Windows\r\n)和%n(注意:这个“占位符”不需要任何数据作为参数,Java将根据操作系统提供正确的序列)。

有关支持的语法的更多信息,请参见格式类的留档。

String leftAlignFormat = "| %-15s | %-4d |%n";

System.out.format("+-----------------+------+%n");
System.out.format("| Column name     | ID   |%n");
System.out.format("+-----------------+------+%n");
for (int i = 0; i < 5; i++) {
    System.out.format(leftAlignFormat, "some data" + i, i * i);
}
System.out.format("+-----------------+------+%n");

输出

+-----------------+------+
| Column name     | ID   |
+-----------------+------+
| some data0      | 0    |
| some data1      | 1    |
| some data2      | 4    |
| some data3      | 9    |
| some data4      | 16   |
+-----------------+------+

匿名用户

试试这个替代方案:asciable。

它提供了文本表格的几种实现,最初使用ASCII和UTF-8字符作为边框。

下面是一个示例表:

    ┌──────────────────────────────────────────────────────────────────────────┐
    │ Table Heading                                                            │
    ├──────────────────┬──────────────────┬──────────────────┬─────────────────┤
    │ first row (col1) │ with some        │ and more         │ even more       │
    │                  │ information      │ information      │                 │
    ├──────────────────┼──────────────────┼──────────────────┼─────────────────┤
    │ second row       │ with some        │ and more         │ even more       │
    │ (col1)           │ information      │ information      │                 │
    │                  │ (col2)           │ (col3)           │                 │
    └──────────────────┴──────────────────┴──────────────────┴─────────────────┘

查找最新版本:http://mvnrepository.com/artifact/de.vandermeer/asciitable

另请参阅:https://stackoverflow.com/a/39806611/363573

匿名用户

我专门为此创建的类是完全动态的:https://github.com/2xsaiko/crogamp/blob/master/src/com/github/mrebhan/crogamp/cli/TableList.java

你可以这样使用它:

TableList tl = new TableList(3, "ID", "String 1", "String 2").sortBy(0).withUnicode(true);
// from a list
yourListOrWhatever.forEach(element -> tl.addRow(element.getID(), element.getS1(), element.getS2()));
// or manually
tl.addRow("Hi", "I am", "Bob");

tl.print();

使用unicode字符会看起来像这样(注意:在控制台中会更好,因为所有字符都是一样宽的):

┌─────────┬─────────────────────────────────────────────────────────────────────────┬────────────────────────────┐
│ Command │ Description                                                             │ Syntax                     │
┢━━━━━━━━━╈━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╈━━━━━━━━━━━━━━━━━━━━━━━━━━━━┪
┃ bye     ┃ Quits the application.                                                  ┃                            ┃
┃ ga      ┃ Adds the specified game.                                                ┃ <id> <description> <path>  ┃
┃ gl      ┃ Lists all currently added games                                         ┃ [pattern]                  ┃
┃ gr      ┃ Rebuilds the files of the currently active game.                        ┃                            ┃
┃ gs      ┃ Selects the specified game.                                             ┃ <id>                       ┃
┃ help    ┃ Lists all available commands.                                           ┃ [pattern]                  ┃
┃ license ┃ Displays licensing info.                                                ┃                            ┃
┃ ma      ┃ Adds a mod to the currently active game.                                ┃ <id> <file>                ┃
┃ md      ┃ Deletes the specified mod and removes all associated files.             ┃ <id>                       ┃
┃ me      ┃ Toggles if the selected mod is active.                                  ┃ <id>                       ┃
┃ ml      ┃ Lists all mods for the currently active game.                           ┃ [pattern]                  ┃
┃ mm      ┃ Moves the specified mod to the specified position in the priority list. ┃ <id> <position>            ┃
┃ top kek ┃ Test command. Do not use, may cause death and/or destruction            ┃                            ┃
┃ ucode   ┃ Toggles advanced unicode. (Enhanced characters)                         ┃ [on|true|yes|off|false|no] ┃
┗━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

并且关闭了 unicode 字符(省略 .withUnicode(true)):

Command | Description                                                             | Syntax                    
--------+-------------------------------------------------------------------------+---------------------------
bye     | Quits the application.                                                  |                           
ga      | Adds the specified game.                                                | <id> <description> <path> 
gl      | Lists all currently added games                                         | [pattern]                 
gr      | Rebuilds the files of the currently active game.                        |                           
gs      | Selects the specified game.                                             | <id>                      
help    | Lists all available commands.                                           | [pattern]                 
license | Displays licensing info.                                                |                           
ma      | Adds a mod to the currently active game.                                | <id> <file>               
md      | Deletes the specified mod and removes all associated files.             | <id>                      
me      | Toggles if the selected mod is active.                                  | <id>                      
ml      | Lists all mods for the currently active game.                           | [pattern]                 
mm      | Moves the specified mod to the specified position in the priority list. | <id> <position>           
top kek | Test command. Do not use, may cause death and/or destruction            |                           
ucode   | Toggles advanced unicode. (Enhanced characters)                         | [on|true|yes|off|false|no]