在java中使用printf换行?%n 正确吗? [英] Taking a new line using printf in java? Is %n correct?
问题描述
这里是新手,但我最终无所适从.我已经让我的程序全部正常工作,但在打印时只需要格式化方面的帮助.
newbie here but I am at my wits in end. I have got my program all to work but just need help with the formatting when printing out.
if(count == 3)
System.out.printf ("%-15s %15s %15s %15s %15s %n", n, " is compatible with
",dates[k],dates[k+1],dates[k+2]);
我的输出是
Stacey Francis is compatible with Owen Farrell Jack Clifford Joshua Watkins
我希望我的输出是(不重复 stacey francis 的名字或与"兼容:
I would like my output to be (without repeating stacey francis name or "is compatible with":
Stacey Francis is compatible with Owen Farrell
Jack Clifford
Joshua Watkins
只是想知道如何解决这个问题?
Just wondering how to go about this?
谢谢,
科斯蒂
推荐答案
是的,%n
是 printf 中的换行符.请参阅 java.util.Formatter 的文档,特别是指定的转换表:
Yes, %n
is a newline in printf. See the documentation of java.util.Formatter, specifically the conversion table which specifies:
'n
' 行分隔符 结果是平台特定的行分隔符
'
n
' line separator The result is the platform-specific line separator
您的输出当前仅在结尾处有一个换行符,而不是您似乎想要它们的位置.您需要使用如下格式:
Your output currently only has a linebreak at the end, not at the points that you seem to want them. You would need to use a format like:
"%-15s %15s %15s %n %15s %n %15s %n"
(可能还有一些标签用于对齐).
(and maybe some tabs thrown in there for alignment).
这篇关于在java中使用printf换行?%n 正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!