【问题标题】:How to print a table using System.out.print?如何使用 System.out.print 打印表格?
【发布时间】:2013-12-01 16:27:35
【问题描述】:

我需要帮助来格式化我工作的 Java 应用程序的输出。

输出应该是这样的(evrey line bellow coresponding category):

---------------------------------------------------
|Account number| Savings Balance| Checking Balance|
---------------------------------------------------
|an integer    | a double       |         a double|
---------------------------------------------------
|............  | .........      |      ...........|
---------------------------------------------------

我做了什么:

System.out.println("|--------------| ---------------| ----------------|");
             System.out.println("|Account number| Savings balance| Checking balance|");
            for (int i=0; i<a.length; i++)
            {

            System.out.println("|"+a[i].getAccountNo()+"|"+a[i].getSavingBal()+"|"+a[i].getCheckingBal()+"|");
            System.out.println("|--------------| ---------------| ----------------|");

        }
            System.out.println("|--------------| ---------------| ----------------|");

【问题讨论】:

标签: java formatting console-application


【解决方案1】:

你可以像使用 String.format() 一样

System.out.println(String.format("%03d %7s %7s %11s", val1, val2, val3, val4));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多