Why am I getting an exception?

前端 未结 1 1511
清酒与你
清酒与你 2021-01-25 08:50

Please help. I get this error after the prompts from the Scanner are displayed to the console:

Exception in thread \"main\" java.util.IllegalFormatPrecisionExcep         


        
相关标签:
1条回答
  • A: Because your format specifiers don't match input arguments used in the printf method.

    Use %f rather than %d as the format specifier character for double values

    System.out.printf("Diamond Cost: $%,.2f \n", diamondCost); 
                                          ^
    

    In addition % requires an additional % to escape that character

    System.out.printf("State Tax @ 10%%: $%,.2f \n", stateTax);
    

    Finally, remove the unnecessary dot character

    System.out.printf("Number Ordered: $%,.2d \n", numOrdered);
                                          ^
    

    Read: Formatter javadoc

    0 讨论(0)
提交回复
热议问题