不要用==比较浮点数据,浮点数据都是不精确的,用区间比较.
if (x>0.03 && x<0.05)
//...

解决方案 »

  1.   

    这是一道scjp的题目,告诉我这道题的理论依据。
      

  2.   

    Integer.equals
    public boolean equals(Object obj)Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.
      

  3.   

    至于
    int i=5;
    double d=5.0;
    System.out.print(i==d);//返回true我猜想是进行了隐式的类型转换
      

  4.   

    int i=5;
    double d=5.0;
    System.out.print(i==d);//返回true
    虽然double型是不精确的数据,但是表示整数时可以精确的表示Integer i=new Integer(5);
    Double d=new Double(5);System.out.print(i==d);//返回false   
    该句好像通不过System.out.print(i.equals(d));//返回false
    Integer、Double对象内的数据除了你所给的数值外可能还有其他的数据,还要看对equals()方法是如何定义的
      

  5.   

    我想我已经搞明白了。
    int i=5;
    double d=5.0;
    System.out.print(i==d);//返回true,在用==比较基本数据典型时,JAVA总是自动归一精度,再比较。
    Integer i=new Integer(5);
    Double d=new Double(5);
    System.out.print(i==d);//返回false,==不能比较两个不同类型的对象,编译错。
    System.out.print(i.equals(d));//返回false,equals可以比较两个不同类型的对象,但必然返回false,只有两个对象,类型相同,并且里面的值也相同才返回真。