java.util.IllegalFormatConversionException:f!= java.lang.String错误

import javax.swing.JOptionPane; public class Minutes { public static void main(String[] args) { double BasePlanCost = 20; final double BaseCostPerMinute=0.15; double MinutesUsed = Double.parseDouble(JOptionPane.showInputDialog("Please enter the amount of minutes Used: ")); double CostForMinutes = BaseCostPerMinute * MinutesUsed; double GrandTotal = BasePlanCost + CostForMinutes; JOptionPane.showMessageDialog(null, String.format("$%.2f","**IST Wireless Receipt**","\n","Base Plan Cost:" +BasePlanCost,"/n","Cost For Minutes Used: "+ CostForMinutes,"/n","Grand Total :" +GrandTotal)); } } 

该程序输入用户输入的分钟数,并通过添加CostForMinutes和BasePlanCost计算总计。 CostForMinutes是通过乘以用户输入的分钟数和BaseCostPerMinute来计算的。 输出是由小数点后两位输出的所有数字,并作为收据输出。

当我编译程序时,它允许我输入分钟数,但代码崩溃并给我这个错误

 exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String 

谁能帮我吗?

编辑这就是我希望输出看起来像http://sofzh.miximages.com/java/CubfC.png

你有

 String.format("$%.2f","**IST Wireless Receipt**", 

这意味着你想格式化第二个参数,它是一个使用%.2f的字符串,这是一个不起作用的浮点格式。

您需要将格式重新组织为第一个,并在之后格式化您要格式化的值。

 String.format("**IST Wireless Receipt**%n" + "Base Plan Cost: $%.2f%n" + "Cost For Minutes Used: $%.2f%n" + "Grand Total: $%.2f%n", BasePlanCost, CostForMinutes, GrandTotal) 

尝试将您的消息整理为:

 String message = String.format( "**IST Wireless Receipt** \n" + " Base Plan Cost:$ %.2f \n" + " Cost For Minutes Used: $ %.2f \n" + " Grand Total : $ %.2f", BasePlanCost, CostForMinutes, GrandTotal); JOptionPane.showMessageDialog(null, message); 

我建议你阅读java语言的代码约定

http://www.oracle.com/technetwork/java/codeconventions-135099.html