所以我有这个代码
public class CarInventoryReport
{
public static void main(String[] args)
{
final String HEADING_FMT_STR = "%-25s%13s%13s%13s\n";
final String DATA_FMT_STR = "%-25s%13d%13.0f%13.0f\n";
String item1 = "Mazda RX-8";
int qty1 = 10;
double price1 = 27999.99;
String item2 = "MINI Cooper";
int qty2 = 100;
double price2 = 23000.25;
System.out.printf(HEADING_FMT_STR,
"Item", "Quantity", "Price", "Value");
System.out.printf(HEADING_FMT_STR,
"-----", "-------", "------", "------");
System.out.printf(DATA_FMT_STR,item1,qty1,price1,qty1*price1);
System.out.printf(DATA_FMT_STR,item2,qty2,price2,qty2*price2);
} // end main
} // end class CarInventoryReport
输出:
Item Quantity Price Value
----- ------- ------ ------
Mazda RX-8 10 28000 280000
MINI Cooper 100 23000 2300025
我不知道这两条线是如何形成的
final String HEADING_FMT_STR = "%-25s%13s%13s%13s\n";
final String DATA_FMT_STR = "%-25s%13d%13.0f%13.0f\n";
更具体地说,部分:这是如何格式化的,它如何转化为使输出有效?
"%-25s%13s%13s%13s\n"
"%-25s%13d%13.0f%13.0f\n"
此外,我还想弄清楚如何获取值以显示在我的值列中的逗号。我需要添加什么才能让它们显示出来?
答案 0 :(得分:1)
以下内容将在“值”列中打印逗号(请注意在DATA_FMT_STR中包含逗号)。
final String DATA_FMT_STR = "%-25s%13d%13.0f%,13.0f\n";
输出:
Item Quantity Price Value
----- ------- ------ ------
Mazda RX-8 10 28000 280,000
MINI Cooper 100 23000 2,300,025