【问题标题】:How to format output console columns in java?如何在java中格式化输出控制台列?
【发布时间】:2018-05-02 03:14:58
【问题描述】:

我编写了一个创建发票的程序,但是当它出现数千个数字时,输出并不整洁并且会破坏一切。我该如何解决这个问题,以便程序的列与这样的数字更加一致?这是我用来创建程序的代码。如果有人可以提供帮助,那将非常有用。

这里是main方法...

public class InvoicePrinter
{
public static void main(String[] args)
  {

  Address samsAddress=new Address("Sam's Small Appliances", "100 Main 
  Street", "Anytown", "CA", "98765");
  Invoice samsInvoice =new Invoice(samsAddress);
  samsInvoice.add(new Product("Toaster", 29.95),3);
  samsInvoice.add(new Product("Hair Dryer", 24.95),1);      
  samsInvoice.add(new Product("Car Vacuum",19.99),2);
  samsInvoice.add(new Product("Nano Parts",100000),1);
  samsInvoice.addSimple(new Product("Shipping",5.00));

  System.out.println(samsInvoice.format());
  }
}

这些是程序运行所需的其他程序

import java.util.ArrayList;

public class Invoice
{
  public Invoice(Address anAddress)
  {
  items=new ArrayList<LineItem>();
  billingAddress=anAddress;
  simpleItems= new ArrayList<SimpleLineItem>();
}

public void addSimple(Product aProduct)
{
  SimpleLineItem anItem= new SimpleLineItem(aProduct);
  simpleItems.add(anItem);
}


public void add(Product aProduct, int quantity)
{
  LineItem anItem=new LineItem(aProduct,quantity);
  items.add(anItem);
}   

 public String format()
 {
  String r="                                                 I N V O I C E\n\n"+billingAddress.format()+String.format("\n\n%-30s%8s%5s%8s\n","Description", "Price","Qty","Total");

  for(LineItem i:items)
  {
     r=r+i.format()+"\n";
  }

  for(SimpleLineItem j:simpleItems)
  {
   r=r+j.format() + "\n";
  }

  r = r + String.format("\nAMOUNT DUE: $%8.2f", getAmountDue());
  return r;
  }

 public double getAmountDue()
 {
  double amountDue = 0;
  for (LineItem i : items)
  {
      amountDue = amountDue + i.getTotalPrice();
  }
  for(SimpleLineItem j:simpleItems)
  {
     amountDue = amountDue + j.getPrice();
  }
 return amountDue; 
  }     


 private Address billingAddress;
 private ArrayList<LineItem> items;
 private ArrayList<SimpleLineItem> simpleItems;
}

还有一些

public class LineItem
{
 public LineItem(Product aProduct, int aQuantity)
 {
  theProduct = aProduct;
  quantity = aQuantity;
 }

 public double getTotalPrice()
 {
  return theProduct.getPrice() *quantity;
 }

 public String format()
 {
  return String.format("%'-30s%'8.2f%'5d%'8.2f", theProduct.getDescription(),theProduct.getPrice(),quantity,getTotalPrice());
 }

 private int quantity;
 private Product theProduct;
}

另一个

public class SimpleLineItem
{
  public SimpleLineItem(Product aProduct)
  {
     theProduct=aProduct;
  }

  public double getPrice()
  {
     return theProduct.getPrice();   
  }

  public String format()
  {
    return String.format("%-30s" +"             " + "%8.2f",
    theProduct.getDescription(), theProduct.getPrice());
  }

  private Product theProduct;
  }  

还有两个

  public class Product
  {  
   public Product(String aDescription,double aPrice)
   {
     description = aDescription;
     price = aPrice;
   }

   public String getDescription()
   {
     return description;
   }

   public double getPrice()
   {
     return price;
   }

   private String description;
   private double price;
   }

最后一个

   public class Address
   {
     public Address(String aName, String aStreet, String aCity, String 
     aState,String aZip)
    {
       name = aName;
       street = aStreet;
       city = aCity;
       state = aState;
       zip = aZip;
    }

   public String format()
   {
     return name + "\n" + street + "\n" + city + ", " + state + " " + zip;
   }

   private String name;
   private String street;
   private String city;
   private String state;
   private String zip;
  }

【问题讨论】:

  • 我尝试运行您的代码并引发异常:线程“main”中的异常 java.util.UnknownFormatConversionException: Conversion = '''

标签: java format invoice


【解决方案1】:

也许你可以看一下 Oracle 关于 System.out.format 和 DecimalFormat 类的 javadocs

Formatting Numeric Print Output

【讨论】:

    【解决方案2】:

    因此,当您在打印出所有内容之前无法确定数字列的总长度时,基本上就会发生这种情况。为此,您需要将数字列的长度设置为最长的数字或在您的情况下为价格长度,并正确证明所有数字。因此,您需要将所有数字添加到数组中并遍历它们以找到最长的数字。

    【讨论】:

    • 像一个int数组?我该怎么做?
    • 不,你使用价格,所以将它们放入一个双数组。在打印整个表格之前,循环遍历这个数组并获取一个数字的最大字符长度。为列值创建必要的缩进
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2013-11-19
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多