Java println formatting so I can display a table?

后端 未结 2 447
误落风尘
误落风尘 2020-12-10 04:05

How can I utilize System.out.print(ln/f) in a way that will allow me to format my output into a table?

If I\'m to use printf, what formatti

相关标签:
2条回答
  • 2020-12-10 04:57

    you can println("\t") which prints a tab, it will align everything easily.

    0 讨论(0)
  • 2020-12-10 05:01

    Yes, since Java 5, the PrintStream class used for System.out has the printf method, so that you can use string formatting.


    Update:

    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);
    
    0 讨论(0)
提交回复
热议问题