java.util.IllegalFormatConversionException:f!= java.lang.String错误 [英] java.util.IllegalFormatConversionException: f != java.lang.String Error

查看:837
本文介绍了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来计算的。输出是由小数点后两位输出的所有数字,并作为收据输出。

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.

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

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?

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

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

推荐答案

你有

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

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

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

你需要重新组织您的格式为第一个以及您想要格式化的值。

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)

这篇关于java.util.IllegalFormatConversionException:f!= java.lang.String错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆