我想将状态消息打印到Console中。
System.out.format("%-60s%10s\n", "Status 1", "[Success]");
System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
System.out.format("%-60s%10s\n", "Status 2 and some text", "[Failed]");
System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
System.out.format("%-60s%10s\n", "Status 2", "[Success]");
System.out.format("%-60s%10s\n", "Status 2 and some very long text", "[Failed]");
System.out.format("%-60s%10s\n", "Status 2", "[Success]");
System.out.format("%-60s%10s\n", "Status 2 and some very very very long text", "[Failed]");
System.out.format("%-60s%10s\n", "Status 2", "[Success]");
System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
但正如您所看到的,第二列消息格式不正确。
Status 1 [Success]
Status 2 [Failed]
Status 2 [Failed]
Status 2 and some text [Failed]
Status 2 [Failed]
Status 2 [Success]
Status 2 and some very long text [Failed]
Status 2 [Success]
Status 2 and some very very very long text [Failed]
Status 2 [Success]
Status 2 [Failed]
我想打印这样的消息:
Status 1 [Success]
Status 2 [Failed]
Status 2 [Failed]
Status 2 and some text [Failed]
Status 2 [Failed]
Status 2 [Success]
Status 2 and some very long text [Failed]
Status 2 [Success]
Status 2 and some very very very long text [Failed]
Status 2 [Success]
Status 2 [Failed]
我怎么能这样做?
答案 0 :(得分:2)
为什么不添加"-"
使最后一个字段左对齐,作为第一个字段?我想"%-60s%-10s\n"
会起作用。
答案 1 :(得分:0)
您可以尝试此代码
System.out.format("%-60s%.10s\n", "Status 1", "[Success]");
System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
System.out.format("%-60s%.10s\n", "Status 2 and some text", "[Failed]");
System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
System.out.format("%-60s%.10s\n", "Status 2 and some very long text", "[Failed]");
System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
System.out.format("%-60s%.10s\n", "Status 2 and some very very very long text", "[Failed]");
System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");