Psh*_*emo 65
您可以使用System.out.format()或System.out.printf()(printf内部只是调用,format因此两个方法都给出相同的结果).
下面是一个示例,它将文本左对齐并用空格填充未使用的位置.可以使用向左对齐字符串%-15s,这意味着:
% 保留(占位符)15 角色的"地方" s String数据类型 - 并从左侧开始打印. 如果要处理数字,请使用d后缀,例如%-4d应放在列左侧的最多4位数字.
BTW printf在打印数据后不会自动添加行分隔符,因此如果想要将光标移动到下一行,我们需要自己完成.我们可以使用\ror \n,或者让Formatter生成OS依赖行分隔符(比如Windows \r\n)我们可以使用%n占位符(注意:它不需要任何数据作为参数,Java将提供基于OS的正确序列).
您可以Formatter在该类的文档中找到有关语法支持的更多信息.
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");
Run Code Online (Sandbox Code Playgroud)
产量
+-----------------+------+
| Column name | ID |
+-----------------+------+
| some data0 | 0 |
| some data1 | 1 |
| some data2 | 4 |
| some data3 | 9 |
| some data4 | 16 |
+-----------------+------+
Run Code Online (Sandbox Code Playgroud)
Ste*_*han 27
试试这个选择:asciitable.
它提供了几种文本表的实现,最初使用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/MRebhan/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();
Run Code Online (Sandbox Code Playgroud)
使用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] ?
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
并且unicode chars off(省略.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]
Run Code Online (Sandbox Code Playgroud)
使用 System.out.printf()
例如,
String s = //Any string
System.out.printf(%10s, s);
Run Code Online (Sandbox Code Playgroud)
将打印出String的内容,正好占用10个字符.因此,如果您想要一个表,只需确保表中的每个单元格打印出相同的长度.另请注意,printf()不会打印新行,因此您必须自己打印.
| 归档时间: |
|
| 查看次数: |
46758 次 |
| 最近记录: |