双值的 Java MethodOverloading 错误
这是我遇到问题的代码的一部分:
===Class Overload===
public class Overload
{
public void testOverLoadeds()
{
System.out.printf("Square of integer 7 is %d\n",square(7));
System.out.printf("Square of double 7.5 is %d\n",square(7.5));
}//..end testOverloadeds
public int square(int intValue)
{
System.out. printf("\nCalled square with int argument: %d\n",intValue);
return intValue * intValue;
}//..end square int
public double square(double doubleValue)
{
System.out.printf("\nCalled square with double argument: %d\n", doubleValue);
return doubleValue * doubleValue;
}//..end square double
}//..end class overload
===Main===
public static void main(String[] args) {
Overload methodOverload = new Overload();
methodOverload.testOverLoadeds(); }
它编译时没有错误,但是当我尝试运行它时,输出是:
使用 int 参数调用 square:7
整数 7 的平方是 49线程“main”中出现异常 java.util.IllegalFormatConversionException: d != java.lang.Double 在 java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999) 在 java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709) 在 java.util.Formatter$FormatSpecifier.print(Formatter.java:2661) 在 java.util.Formatter.format(Formatter.java:2433) 在 java.io.PrintStream.format(PrintStream.java:920) 在 java.io.PrintStream.printf(PrintStream.java:821) 在 methodoverload.Overload.square(Overload.java:19) 在 methodoverload.Overload.testOverLoadeds(Overload.java:8) 在方法重载.Main.main(Main.java:9) 使用双参数调用 square:Java 结果:1
我做错了什么?
我使用的是 Ubuntu 10.10、Netbeans 6.9。
谢谢。
Here is a part of my code I'm having trouble with:
===Class Overload===
public class Overload
{
public void testOverLoadeds()
{
System.out.printf("Square of integer 7 is %d\n",square(7));
System.out.printf("Square of double 7.5 is %d\n",square(7.5));
}//..end testOverloadeds
public int square(int intValue)
{
System.out. printf("\nCalled square with int argument: %d\n",intValue);
return intValue * intValue;
}//..end square int
public double square(double doubleValue)
{
System.out.printf("\nCalled square with double argument: %d\n", doubleValue);
return doubleValue * doubleValue;
}//..end square double
}//..end class overload
===Main===
public static void main(String[] args) {
Overload methodOverload = new Overload();
methodOverload.testOverLoadeds(); }
It compiles with no error, however when I try to run it the output is:
Called square with int argument: 7
Square of integer 7 is 49Exception 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 methodoverload.Overload.square(Overload.java:19)
at methodoverload.Overload.testOverLoadeds(Overload.java:8)
at methodoverload.Main.main(Main.java:9)
Called square with double argument:Java Result: 1
What am I doing wrong?
I'm on Ubuntu 10.10, Netbeans 6.9.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
双精度值的格式说明符是 f,而不是 d。所以这一行应该是:
有关格式说明符的更多详细信息 这里。
The format specifier for double values is f, not d. So this line should be:
More details about the format specifiers here.
试试这个
您需要更改双重格式化程序“%d\n”
try this
You need to change the double formatter "%d\n"