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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35279083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:56:20  来源:igfitidea点击:

java.util.IllegalFormatConversionException: f != java.lang.String Error

javaswingjoptionpane

提问by Jeff

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));

    }

}

This program inputs the amount of minutes the user enters and calculates the grand total by adding the CostForMinutes and BasePlanCost. CostForMinutes is calculated by multiplying the minutes the user enters and the BaseCostPerMinute. The out is all the numbers outputted by two decimal places and outputted as a receipt.

该程序输入用户输入的分钟数,并通过将 CostForMinutes 和 BasePlanCost 相加来计算总计。CostForMinutes 的计算方法是将用户输入的分钟数与 BaseCostPerMinute 相乘。out是所有输出两位小数并作为收据输出的数字。

When I compile the program it lets me input the amount of minutes but the code collapses and gives me this error

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

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

can anyone help me out?

谁能帮我吗?

EDIT this is what I want the output to look like http://i.stack.imgur.com/CubfC.png

编辑这是我希望输出看起来像 http://i.stack.imgur.com/CubfC.png

回答by Peter Lawrey

You have

你有

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

This means you want to format the second argument which is a String using %.2fwhich is a float format which won't work.

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

You need to re-organize your format to be first and the values you want to format after it.

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

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

回答by Jesus Zavarce

Try to organize your message as:

尝试将您的消息组织为:

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);

I recommended you to read the code conventions of the java language

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

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

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