我有格式问题,不确定如何解决。
基本上我得到的输出是:
ID Name Price Quantity
1 Coke £0.70 5
2 Fanta £0.60 5
3 Galaxy £1.20 5
4 Snickers £1.00 5
5 Dairy Milk £1.30 5
我正在寻找一切以便正确排列。
这是我的代码:
public void itemList() {
VendItem[] itemList = stock;
System.out.println("List All Items");
System.out.println("++++++++++++++\n");
System.out.println("ID\t" + "Name\t " + "Price\t" + "Quantity");
for (int i = 0; i < itemCount; i++) {
System.out.print(i + 1 + "\t");
System.out.print(itemList[i].getName() +"\t ");
System.out.printf("£%.2f", itemList[i].getPrice());
System.out.print("\t");
System.out.print(itemList[i].getQty() +"\n");
}
这里有个小窍门。由于您要创建一个表头,该表头描述每列,因此请首先将表头的格式设置为您希望表显示的方式。基本上,这将为您提供一个很好的主意,即如何设置将在该表中显示的所有数据的格式(可能会进行一些小的改进)。
而不是仅打印标题行并使用空格(“”)或制表符(“ \ t”){heaven forebid} 分隔列名,而是使用String#format()方法。我说的是String#format()方法而不是Console#printf()方法,因为我们希望稍后使用标头长度来在该标头下方创建下划线。这是一个例子:
String header = String.format("%-6s %-15s %-10s %-4s", "ID", "Name", "Price", "Quantity");
System.out.println(header);
// Underline Header. Using the String#join() method for this.
System.out.println(String.join("", Collections.nCopies(header.length(), "=")));
控制台输出将类似于:
ID Name Price Quantity
==========================================
看起来不错,我们将使用它。为了创建Header下划线,我们使用String#join()方法和Collections#nCopies()方法。
现在是时候以完全相同的格式在Header下显示表数据了。为此,请在for循环中仅使用“一个” printf(),如下所示:
for (int i = 0; i < itemList.size(); i++) {
System.out.printf("%-6d %-15s £%-10.2f %-4d%n",
itemList.get(i).id,
itemList.get(i).name,
itemList.get(i).price,
itemList.get(i).quantity);
}
通知如何格式字符串的内使用printf()的方法是相同的格式字符串的内使用字符串#格式()方法用的是,异常的printf()的格式字符串还含有%N标记。该标记用于生成特定于平台的行分隔符,因为我们希望将for循环的下一次迭代打印在控制台窗口中的新行上。使用您在帖子中提供的数据,控制台的输出应如下所示:
ID Name Price Quantity
==========================================
1 Coke £0.70 5
2 Fanta £0.60 5
3 Galaxy £1.20 5
4 Snickers £1.00 5
5 Dairy Milk £1.30 5
看起来还可以,但是,如果“数量”值在标题名称“数量”下更居中,那就太好了。这可以通过调整标签所用的整体间距来表示“价格”来实现,以便使其宽度稍宽一些。只需在该格式字符串标记中添加2即可将其改为12而不是10,如下所示:
for (int i = 0; i < itemList.size(); i++) {
System.out.printf("%-6d %-15s £%-12.2f %-4d%n",
itemList.get(i).id,
itemList.get(i).name,
itemList.get(i).price,
itemList.get(i).quantity);
}
现在输出将如下所示:
ID Name Price Quantity
==========================================
1 Coke £0.70 5
2 Fanta £0.60 5
3 Galaxy £1.20 5
4 Snickers £1.00 5
5 Dairy Milk £1.30 5
妳去 您可能已经注意到,我一直在迭代的数据包含在一个集合中。我认为使用ArrayList或List接口是代替数组的更好方法,因为这些列表可以根据需要增长,而不是需要像Array这样预先初始化为特定大小。
这是我用于这些演示的VendItem类:
import java.util.Collections;
public class VendItem {
private int id;
private String name;
private float price;
private int quantity;
public static String HEADER = getHeader();
public VendItem() { }
public VendItem(int id, String name, float price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "id=" + id + ", name=" + name + ", price=" + price + ", quantity=" + quantity;
}
private static String getHeader() {
HEADER = String.format("%-6s %-15s %-10s %-4s", "ID", "Name", "Price", "Quantity") +
System.lineSeparator();
HEADER += String.join("", Collections.nCopies(HEADER.length(), "="));
return HEADER;
}
public String toFormattedString() {
return String.format("%-6d %-15s £%-12.2f %-4d", id, name, price, quantity);
}
}
这是我用来显示演示的代码:
List<VendItem> itemList = new ArrayList<>();
itemList.add(new VendItem(1, "Coke", 0.70f, 5));
itemList.add(new VendItem(2, "Fanta", 0.60f, 5));
itemList.add(new VendItem(3, "Galaxy", 1.20f, 5));
itemList.add(new VendItem(4, "Snickers", 1.00f, 5));
itemList.add(new VendItem(5, "Dairy Milk", 1.30f, 5));
// Display Title
System.out.println("List All Items");
System.out.println("++++++++++++++");
System.out.println()
// Display Table Header.
String header = String.format("%-6s %-15s %-10s %-4s", "ID", "Name", "Price", "Quantity");
System.out.println(header);
// Underline the Header.
System.out.println(String.join("", Collections.nCopies(header.length(), "=")));
// Display Table Data...
for (int i = 0; i < itemList.size(); i++) {
System.out.printf("%-6d %-15s £%-12.2f %-4d%n",
itemList.get(i).id,
itemList.get(i).name,
itemList.get(i).price,
itemList.get(i).quantity);
}
编辑:只是另一个想法:
您可以使VendItem类返回Header和格式化的数据字符串,例如toString()方法。提供的类已经包含一个toString()方法,但是如果您添加了toFormatedString()方法,那么它将使显示更加容易。我在类中添加了HEADER字段,并添加了toFormattedString()。使用这些附加的类项目,您的表可以像这样显示在控制台上:
List<VendItem> itemList = new ArrayList<>();
itemList.add(new VendItem(1, "Coke", 0.70f, 5));
itemList.add(new VendItem(2, "Fanta", 0.60f, 5));
itemList.add(new VendItem(3, "Galaxy", 1.20f, 5));
itemList.add(new VendItem(4, "Snickers", 1.00f, 5));
itemList.add(new VendItem(5, "Dairy Milk", 1.30f, 5));
System.out.println("List All Items");
System.out.println("++++++++++++++");
System.out.println();
System.out.println(VendItem.HEADER);
for (VendItem item : itemList) {
System.out.println(item.toFormattedString());
}
是不是容易得多。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句