Java方法使用双精度值加载错误 [英] Java MethodOverloading error with double values

查看:128
本文介绍了Java方法使用双精度值加载错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码的一部分,我有麻烦:
$ b

===类重载===

  public class重载
{
public void testOverLoadeds()
{
System.out.printf( 7是%d \ n,square(7));
System.out.printf(Double 7.5的平方是%d \ n,square(7.5));
} // .. end testOverloadeds

public int square(int intValue)
{


System.out。 printf(\ nCalled广场与int参数:%d \ n,intValue);
return intValue * intValue;
} // .. end square int

public double square(double doubleValue)
{
System.out.printf(\\\
called square with double argument :%d \\\
,doubleValue);
返回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 b / b
$ b在线程main中的异常
java.util.IllegalFormatConversionException:
d!= java.lang.Double $ b $ java.util.Formatter $ FormatSpecifier.failConversion(Formatter.java: 3999)在$ java.util.Formatter
$ FormatSpecifier.printInteger(Formatter.java:2709)$ b $在java.util.Formatter $ FormatSpecifier.print(Formatter.java:2661)$ b $在Java .util.Formatter.format(Formatter.java:2433)
位于java.io.PrintStream.format(PrintStream.java:920)
位于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)
带双参数的被调方块:Java结果:1


我在做什么错?



我在Ubuntu 10.10上,Netbeans 6.9。



谢谢。

解决方案

double值的格式说明符是f,而不是d。所以这行应该是:
$ b $ pre $ System.out.printf(\\\
called square with double argument:%f \\\
,doubleValue);

关于格式说明符的更多细节这里


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.

解决方案

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.

这篇关于Java方法使用双精度值加载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆