双值的 Java MethodOverloading 错误

发布于 2024-10-09 16:19:18 字数 1745 浏览 0 评论 0原文

这是我遇到问题的代码的一部分:

===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 49

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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-10-16 16:19:18

双精度值的格式说明符是 f,而不是 d。所以这一行应该是:

 System.out.printf("\nCalled square with double argument: %f\n", doubleValue);

有关格式说明符的更多详细信息 这里

The format specifier for double values is f, not d. So this line should be:

 System.out.printf("\nCalled square with double argument: %f\n", doubleValue);

More details about the format specifiers here.

断舍离 2024-10-16 16:19:18

试试这个

   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 %.4f%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: %.4f%n",doubleValue);
        return doubleValue * doubleValue;

    }// ..end square double
    // ..end class overload

    public static void main(String[] args) {

        Overload methodOverload = new Overload();
        methodOverload.testOverLoadeds();

    }
}

您需要更改双重格式化程序“%d\n”

try this

   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 %.4f%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: %.4f%n",doubleValue);
        return doubleValue * doubleValue;

    }// ..end square double
    // ..end class overload

    public static void main(String[] args) {

        Overload methodOverload = new Overload();
        methodOverload.testOverLoadeds();

    }
}

You need to change the double formatter "%d\n"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文