java println格式化我可以显示一个表吗? [英] Java println formatting so I can display a table?
本文介绍了java println格式化我可以显示一个表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 System.out.print(ln / f)
,以允许我将输出格式化为表格?
How can I utilize System.out.print(ln/f)
in a way that will allow me to format my output into a table?
如果我要使用 printf
,我应该指定什么格式来实现以下结果?
If I'm to use printf
, what formatting should I specify to achieve the below results?
我想打印的示例表:
n result1 result2 time1 time2
-----------------------------------------------------
5 1000.00 20000.0 1000ms 1250ms
5 1000.00 20000.0 1000ms 1250ms
5 1000.00 20000.0 1000ms 1250ms
$ b $
推荐答案
是的,从Java 5开始, PrintStream
用于的类System.out
具有 printf
方法,以便您可以使用字符串格式。
Yes, since Java 5, the PrintStream
class used for System.out
has the printf
method, so that you can use string formatting.
更新:
实际的格式化命令取决于您要打印的数据,您想要的确切间距等。以下是许多可能的示例之一:
The actual formatting commands depend on the data you are printing, the exact spacing you want, etc. Here's one of many possible examples:
System.out.printf("%1s %-7s %-7s %-6s %-6s%n", "n", "result1", "result2", "time1", "time2");
System.out.printf("%1d %7.2f %7.1f %4dms %4dms%n", 5, 1000F, 20000F, 1000, 1250);
System.out.printf("%1d %7.2f %7.1f %4dms %4dms%n", 6, 300F, 700F, 200, 950);
这篇关于java println格式化我可以显示一个表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文