我希望格式化此代码:
Object[][] table = new String[4][];
table[0] = new String[] { "Pie", "2.2", "12" };
table[1] = new String[] { "Cracker", "4", "15" };
table[2] = new String[] { "Pop tarts", "1", "4" };
table[3] = new String[] { "Sun Chips", "5", "2" };
String itemId = CommandLine.Help.Ansi.AUTO.string("@|bold,green,underline ItemId|@");
System.out.format("%-15s%-15s%-15s%-15sn", itemId, "Item", "Price", "Quantity");
for (int i = 0; i < table.length; i++) {
Object[] row = table[i];
System.out.format("%-15d%-15s%-15s%-15sn", i, row[0], row[1], row[2]);
}
这是早期堆栈交换问题(java格式化表格输出(的衍生示例
然而,使用我的标记字符串,而不是获得正确的选项卡式标题,我得到了这个。
怎么回事?
奇怪,我觉得不错。
代码:
import picocli.CommandLine;
public static void main(String... args) {
Object[][] table = new String[4][];
table[0] = new String[]{"Pie", "2.2", "12"};
table[1] = new String[]{"Cracker", "4", "15"};
table[2] = new String[]{"Pop tarts", "1", "4"};
table[3] = new String[]{"Sun Chips", "5", "2"};
String itemId = CommandLine.Help.Ansi.AUTO.string("@|bold,green,underline ItemId|@");
System.out.format("%-15s%-15s%-15s%-15sn", itemId, "Item", "Price", "Quantity");
for (int i = 0; i < table.length; i++) {
Object[] row = table[i];
System.out.format("%-15d%-15s%-15s%-15sn", i, row[0], row[1], row[2]);
}
} ```
output:
ItemId Item Price Quantity
0 Pie 2.2 12
1 Cracker 4 15
2 Pop tarts 1 4
3 Sun Chips 5 2