java System.out.print格式样式

时间:2016-10-22 08:11:42

标签: java printing

System.out.println(tree.item +"\t\t\t"+tree.list);
as            [26, 27, 38]
associated            [1]
at            [17, 18] 
attack        [10]

但我想要像这样打印

as            [26, 27, 38]
associated    [1]
at            [17, 18] 
attack        [10]

1 个答案:

答案 0 :(得分:1)

我无法访问您想要的代码,但我建议您使用 - 这意味着基本上留下缩进

实施例

String[] item = new String[] { "Audi", "BMW", "VW", "Ferrari", "Cherry QQ" };
float[] price = new float[] { 18000.0f, 24000.0f, 2100.50f, 786400.1f, 120.70f };

for (int i = 0; i < price.length; i++) {
    System.out.printf("%2d. %-10s €%.2f\n", i + 1, item[i], price[i]);
}

结果看起来像

一样左对齐

enter image description here