java.util.IllegalFormatConversionException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22668614/
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
java.util.IllegalFormatConversionException
提问by user3465376
I am having a little problem in my code here. I have no idea how I am supposed to fix it, and I tried some stuff, but I think, I'm not getting the message here, even though I suspect the issue to be some kind of elemental and easy to fix problem. The exceptions are below the code.
我的代码在这里有一个小问题。我不知道我应该如何修复它,我尝试了一些东西,但我想,我没有在这里收到消息,即使我怀疑这个问题是某种基本的和易于修复的问题。例外情况在代码下方。
package test;
public class CircleExercise {
public static void main(String[] args) {
double[] rKreis = new double[3];
for(int i = 1 ; i <= 3 ; i++){
rKreis[i] = Double.parseDouble("4.9");
System.out.printf("%n%d, Kreis: %nRadius: %d%nUmfang: %d%nFlaeche: %d%n",
i, rKreis[i], Circle.getCircumference(rKreis[i]), Circle.getArea(rKreis[i]));
}
}
}
Exception is as follows
异常如下
1, Kreis:
Radius: Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2661)
at java.util.Formatter.format(Formatter.java:2433)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at CircleExercise.main(CircleExercise.java:13)
回答by Sabuj Hassan
%d
goes with integer in Java. Use %f
instead in your printf()
%d
与Java中的整数一起使用。使用%f
,而不是在你的printf()
Another useful info. If you use %.02f
then it will print only two decimal point value after the dot .
另一个有用的信息。如果你使用%.02f
那么它只会在点后打印两个小数点值.