线程“main”中的异常 java.util.IllegalFormatConversionException: d != java.lang.String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26441216/
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 09:55:59  来源:igfitidea点击:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String

java

提问by elleninnit

I'm new to Java and I'm not sure how to fix the error I'm getting when trying to run this code:

我是 Java 新手,我不确定如何解决我在尝试运行此代码时遇到的错误:

import java.util.Scanner;
public class P3_3 
{

    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int number = in.nextInt();
        if (number < 0) number *= -1;
        if (number >= 10 && number < 100)
        {
            number = 2;
        }
        else if (number >= 100 && number < 1000)
        {
            number = 3;
        }
        else if (number >= 1000 && number < 10000)
        {
            number = 4;
        }
        else if (number >= 10000 && number < 100000)
        {
            number = 5;
        }
        else if (number >= 100000 && number < 1000000)
        {
            number = 6;
        }
        else if (number >= 1000000 && number < 10000000)
        {
            number = 7;
        }
        else if (number >= 10000000 && number < 100000000)
        {
            number = 8;
        }
        else if (number >= 100000000 && number < 1000000000)
        {
            number = 9;
        }
        else
        {
            System.out.println("Number is greater than 10 billion.");
        }
        System.out.printf("%d has ", number + "digits.");
    }
}

I'm trying to print how many digits the number has, by checking whether the number is >= 10, >= 100and so on... I run the code and I'm able to input an int but once I input it and press enter all I get it an error and can't seem to see what I have done wrong. Can anyone enlighten me?

我正在尝试通过检查数字是否为>= 10, >= 100等来打印数字有多少位数......我运行代码并且我能够输入一个int但是一旦我输入它并按回车我就得到它一个错误,似乎看不出我做错了什么。任何人都可以启发我吗?

回答by Stefano Sanfilippo

The problem is here:

问题在这里:

System.out.printf("%d has ", number + "digits.");

the %dformat specifier requires an integer to be passed as second parameter to printf, but by concatenating numberand "digits.", you actually passed a String.

%d格式说明需要作为第二个参数传递一个整数printf,而是通过连接number"digits.",你居然通过了String

Fixed version:

固定版本:

System.out.printf("has %d digits ", number);

note that you cannot print both the original number and the number of digits, because you overwrote one with the other in the numbervariable. Maybe use two different ones.

请注意,您不能同时打印原始数字和位数,因为您在number变量中用另一个覆盖了一个。也许使用两个不同的。

回答by dan

Like others have said, the issue was in the print statement. A better approach, instead of using all the if statements you should use the log10 method:

正如其他人所说,问题出在印刷声明中。一个更好的方法,而不是使用所有的 if 语句,您应该使用 log10 方法:

public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int number = in.nextInt();
        if (number < 0) number *= -1;
        System.out.printf("%d has %d digits\n", number, (int)Math.log10(number) + 1 );
}

回答by SMA

Change your last printf to:

将您的最后一个 printf 更改为:

System.out.printf("number has %d digits ", number);

Try to append all the string together and use specifier i.e.

尝试将所有字符串附加在一起并使用说明符即

System.out.printf("blah %d blah ", value).