首页 > 解决方案 > Printf 格式问题。不会对齐

问题描述

我在这里查看了一堆关于我的问题的问题,甚至是关于 Java 的问题,但我一直无法弄清楚。这是我的代码

System.out.printf(
    "%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s\n",
    "Record #",
    "Inventory",
    "Size", "Color", "Cost", "Markup", "Sale Price", "QTY", "SKU #");

    for (int x = 0; x < StoreInventory.inventory.length; ++x) {
        System.out.printf(
           "%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s\n", x + 1,
            StoreInventory.inventory[x][0],
            StoreInventory.inventory[x][1],
            StoreInventory.inventory[x][2],
            StoreInventory.buyPrice[x],
            StoreInventory.markUp[x],
            StoreInventory.storeSalePrice[x],
            StoreInventory.quantity[x],
            StoreInventory.skuNumber[x]);
}

输出是:

Record #             Inventory            Size                           Color                Cost                 Markup               Sale Price           QTY                  SKU #               
1                    SHORTS               XL                   BLACK                5.0                  10.0                 50.0                 10                   SL-1234AE     

所有字段都是用户输入。无论输入的大小如何,我需要更改什么来对齐输出?

标签: javaprintf

解决方案


推荐阅读