【问题标题】:How to display a list of bill?如何显示账单列表?
【发布时间】:2015-01-25 06:29:37
【问题描述】:

我已经声明了“价格”和“类别”,现在我希望这样显示:

productname     price       id      amount
water           25          3       3(bottles)

(显示账单的输出末尾的列表)

怎么做?

这是我迄今为止尝试过的:

do {
    String Enterproducts=in.next();
    if(Enterproducts.equals("Stop")){
        break;
    }
    int n = in.nextInt();
    for(int i=0; i<category.length; i++){
        if(Enterproducts.equals(category[i])){
            System.out.print(" "+category[i]+" "+price[i]+" "+n);
            System.out.println();
        }
    }
} while(in.hasNext());

【问题讨论】:

  • 您的代码正在运行,但输出未对齐,对吧?是这个问题吗?
  • 是的,如何将所有内容打印在一起?

标签: java formatting billing


【解决方案1】:

试试这个

do {
    String Enterproducts=in.next();
    if(Enterproducts.equals("Stop")){
        break;
    }
    int n = in.nextInt();
    for(int i=0; i<category.length; i++){
        if(Enterproducts.equals(category[i])){
            System.out.print(String.format("%-20s %s",category[i], price[i], n));
            System.out.println();
        }
    }
} while(in.hasNext());

使用%-20s,您指定空间长度。

作为替代方案,您可以像以前一样执行此操作,但使用制表符代替空格(制表符 = \t)。

【讨论】:

    猜你喜欢
    • 2020-12-21
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2021-10-28
    • 2015-09-21
    • 1970-01-01
    相关资源
    最近更新 更多