【问题标题】:"Beautify" Java console output“美化”Java 控制台输出
【发布时间】:2018-04-23 20:41:04
【问题描述】:

我想在控制台中美化我的版本。 我现在如何像在表格中一样写下整个内容? 我试过用空格来计数,但随着值的变化,这非常糟糕。你知道如何美化它们之间的问题吗?

Console output

我的代码:

 private static void berechneVerbrauch(Tankbeleg[] tanken) {
        System.out.print("Datum" + " KM-Stand" + " Liter" + " Preis" + " Verbrauch");
        System.out.println(" ");
        for (int i = 0; i < tanken.length; i++) {
            System.out.print(tanken[i].datum + " " + tanken[i].kmStand + " " + tanken[i].getankteLiter + " " + tanken[i].literPreis+ " " );
            System.out.println(" ");

        }
    }

提前谢谢你

最好的问候 迈克尔

编辑

Console output with \t

private static void berechneVerbrauch(Tankbeleg[] tanken) {
        System.out.print("Datum" + "\tKM-Stand" + "\tLiter" + "\tPreis" + "\tVerbrauch");
        System.out.println(" ");
        for (int i = 0; i < tanken.length; i++) {
            //System.out.printf("%s %s %s %s", tanken[i].datum, tanken[i].kmStand, tanken[i].getankteLiter, tanken[i].literPreis);
            System.out.print(tanken[i].datum + "\t" + tanken[i].kmStand + "\t" + tanken[i].getankteLiter + "\t" + tanken[i].literPreis+ "\t" );
            System.out.println(" ");
        }
    }

【问题讨论】:

    标签: java console output


    【解决方案1】:

    使用 System.out.printf() 。 printf 是格式化打印,因此您可以声明希望每个变量有多少个空格,根据需要截断或扩展变量。请参阅tutorials for printf() 了解如何使用它。

    在你的情况下,这样的标题可能看起来不错:

    System.out.printf("%10s %8s %5s %5s %9s", "Datum", "KM-Stand", "Liter", "Preis", "Verbrauch");
    

    对于数据线:

    System.out.printf("%10s %8s %5s %5s", tanken[i].datum, tanken[i].kmStand, tanken[i].getankteLiter, tanken[i].literPreis );
    

    我写了一个这样的 main 方法来模拟代码,以便我可以看到格式

    public static void main(String[] args){
        System.out.printf("%10s %8s %5s %5s %9s", "Datum", "KM-Stand", "Liter", "Preis", "Verbrauch");
        System.out.println(" ");
        for (int i = 0; i < args.length; i++) {
            System.out.printf("%10s %8s %5s %5s", args[0], args[1], args[2], args[3] );
            System.out.println(" ");
    
        }
    }
    

    :: 输出 ::

    $ java Test 20.05.2010 30000 20 30
         Datum KM-Stand Liter Preis Verbrauch 
    20.05.2010    30000    20    30 
    20.05.2010    30000    20    30 
    20.05.2010    30000    20    30 
    20.05.2010    30000    20    30 
    
    $ java Test alpha beta cala baza
         Datum KM-Stand Liter Preis Verbrauch 
         alpha     beta  cala  baza 
         alpha     beta  cala  baza 
         alpha     beta  cala  baza 
         alpha     beta  cala  baza 
    

    您可以使用printf 选项使它们左对齐或填充除空格之外的其他字符,这是一个非常有用的工具

    【讨论】:

      【解决方案2】:

      你可以使用 tab \t 来代替空格

      【讨论】:

      • 谢谢!请看看我的新编辑。使用 \t 看起来更好,但还不是最优的。
      • 然后把标签翻倍,试试这个 "\t\tKM-Stand" "\t\t" + tanken[i].getankteLiter
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 2010-11-29
      相关资源
      最近更新 更多