2015-02-17 35 views
-1

这是我不断收到错误:异常Java中的printf方法

整个库存的总金额是:在线程$异常“主要” java.util.MissingFormatArgumentException:格式说明“%.2f”

double total = 0.0; 

for (int i = 0; i < 5; i++){ 
    total = total + products[i].getInventoryValue(); 
} 

System.out.printf ("Total value of entire inventory is:$%.2f" + total); 
System.out.println(); 
Arrays.sort (products); 

回答

2
System.out.printf("Total value of entire inventory is:$%.2f" + total); 

应该

System.out.printf("Total value of entire inventory is:$%.2f", total); 

第一个总与字符串连接(以获得单个LO nger字符串"Total value of entire inventory is:$%.2f15.00"),然后将其用作格式字符串(不带参数来替换%.2f)。

第二个实际上让printf格式化字符串。

+0

它完美的作品。谢谢! – Articuno 2015-02-17 23:54:28