我收到System.out.printf()的错误

Bra*_*ner 2 java eclipse printf

我收到运行时错误,我无法弄清楚原因.虽然我已经发现它正在发生,因为下面的代码行.

System.out.printf("\n\nThe total bill for your group is $%.2f before taxes. The HST (13%) will be $%.2f.", totalBill, amountOfTax);
Run Code Online (Sandbox Code Playgroud)

totalBill和amountOfTax都是浮点型变量.

我对编程很新,几乎没有使用printf的经验.我在程序的前面有一个非常相似的代码行,它基本上做同样的事情并且运行正常.

我得到的错误如下:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ')'
    at java.util.Formatter.checkText(Formatter.java:2579)
    at java.util.Formatter.parse(Formatter.java:2555)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at RestaurantOrder.main(RestaurantOrder.java:220)
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助,我很感激!

PS.我应该注意到我正在使用Eclipse Luna(4.4.0)

Lef*_*une 7

您需要转义%sign in format string."13%"变为"13 %%":

System.out.printf("\n\nThe total bill for your group is $%.2f before taxes. " + 
    "The HST (13%%) will be $%.2f.", totalBill, amountOfTax);
Run Code Online (Sandbox Code Playgroud)