繁体   English   中英

不知道为什么我的代码给我一个“非法格式转换异常”错误?

[英]Not sure why my code is giving me an "Illegal Format Conversion Exception" error?

我很确定错误是由最后一行引起的。据我所知,我不高兴我使用“%d”,变量。 但这不是 integer 的有效输入吗?

import java.util.Scanner;

public class total_cost {
    public static void main(String[] args) {

        int TVs;
        int VCRs;
        int controller;
        int CD;
        int recorder;
        double TV_price;
        double VCR_price;
        double controller_price;
        double CD_price;
        double recorder_price;
        double tax;
        {
            TV_price = 400.00;
            VCR_price = 220.00;
            controller_price = 35.20;
            CD_price = 300.00;
            recorder_price = 150.00;
            tax = .0825;

            Scanner in = new Scanner(System.in);
            {
                System.out.printf("How many TV's were sold? ");
                TVs = in.nextInt();

                System.out.printf("How many VCR's were sold? ");
                VCRs = in.nextInt();

                System.out.printf("How many remote controller's were sold? ");
                controller = in.nextInt();

                System.out.printf("How many CD players were sold? ");
                CD = in.nextInt();

                System.out.printf("How many Tape Recorder's were sold? ");
                recorder = in.nextInt();

                System.out.printf("QTY\tDESCRIPTION\tUNIT PRICE\tTOTAL PRICE\n");
                System.out.printf("%d", TVs + "\tTelevision\t%f", TV_price
                        + "\t" + tax * TV_price + "%f", TV_price);
            }
        }
    }
}

错误信息:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4011)
    at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2725)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2677)
    at java.util.Formatter.format(Formatter.java:2449)
    at java.util.Formatter.format(Formatter.java:2383)
    at java.lang.String.format(String.java:2781)
    at total_cost.main(total_cost.java:37)

您传递的是字符串,而不是整数。 应该是这样的:

System.out.printf("%d\tTelevision\t%.2f\t%.2f,%.2f", TVs, TV_price,tax * TV_price, TV_price);

PS:我可以自由地将价格格式化为小数点后两位。

您应该决定是否要使用似乎更适合您的printf()println()

System.out.println(3 + "\tTelevision\t" + TV_price + "\t" + tax * TV_price + " " + TV_price);

使用printf() ,首先要完整地放置格式字符串,然后再放置所有值,如下所示:

System.out.printf("%d number %s string\n", 3, "hi");

由于此行,您将收到此异常:

System.out.printf("%d", TVs + "\\tTelevision\\t%f", TV_price + "\\t" + tax * TV_price + "%f", TV_price);

TVs + "\\tTelevision\\t%f"正在试图做的,因为的算术运算+

您应该格式化输出,然后提供值。

示例: System.out.printf("%d %s", TVs, "\\tTelevision\\");

我对Java并不是很熟悉,但是我所知道的几乎所有“打印格式”功能都首先使用格式字符串,然后使用所有参数遵循一个模式。

像这样

printf("This is a string %d %d %s %f", arg1, arg2, arg3, arg4);

您似乎想做的是同时设置格式字符串和常规串联。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM