简体   繁体   English

为什么“好”一词不显示?

[英]Why doesn't the word “good” display?

When I run this code, why doesn't the word "good" display? 当我运行此代码时,为什么不显示“ good”一词? The value stored in position 0 of Array1 and Array2 is 1 for both. 对于Array1和Array2,存储在位置0的值均为1。

public class ArrayStuff {

    public static void main(String[] args) {

        double value = 1;

        Double[] Array1 = new Double[10];
        Array1 = new Double[] {1.0};

        Double[] Array2 = new Double[10];

        Array2[0] = value;

        int i = 0;

        System.out.println(Array1[0]);
        System.out.println(Array2[i]);

        if (Array1[0] == Array2[i])
        {

            System.out.println("good");

        }
     }

}

In Java, when one asks 在Java中,当有人问

Double d1 = ...;
Double d2 = ...;
if (d1 == d2) {
    System.out.println("Equal");
} else {
    System.out.println("Not Equal");
}

the JRE does not unbox the Double objects, so it checks whether the two Double s are the same exact object. JRE不会取消对Double对象的装箱,因此它将检查两个Double是否是相同的确切对象。 Here, they aren't. 在这里,他们不是。 Note the difference between these two blocks of code: 请注意这两个代码块之间的区别:

Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
if (i1 == i2) {
    System.out.println("Equal");
} else {
    System.out.println("Not Equal");
}

and

Integer i1 = Integer.valueOf(1);
Integer i2 = Integer.valueOf(1);
if (i1 == i2) {
    System.out.println("Equal");
} else {
    System.out.println("Not Equal");
}

The second version reuses the Integer object, so the first prints Not Equal and the second prints Equal . 第二个版本重用Integer对象,因此第一个版本打印Not Equal ,第二个版本打印Equal

You're learning that computers cannot represent floating point numbers to exact precision. 您正在学习计算机无法精确表示浮点数。 The solution is to use code that finds a floating point solution that is "close enough" or else one that uses decimal numbers. 解决方案是使用代码查找“足够接近”或使用十进制数字的浮点解决方案。

Because finite precision numbers have their limits especially with regard to comparing them for exact equality. 因为有限精度数有其局限性,特别是在比较它们的精确相等性方面。

You should approach the problem by checking if the difference between to values is less than a certain threshold: 您应该通过检查to值之间的差异是否小于某个阈值来解决该问题:

public final double EPSILON = 0.001;

if (Math.abs(Array1[0] - Array2[i]) < EPSILON)
 ...

does your first variable has to be a double? 您的第一个变量必须是双精度吗? I mean with an int you solve that problem. 我的意思是说您可以解决这个问题。

This raises a question: does this line actually create the array with 10 slots and assigns the first value: 1.0 to the 0 slot in the array or fails to create the array correctly? 这就提出了一个问题:该行是否实际创建具有10个插槽的数组并为数组中的0插槽分配第一个值:1.0还是无法正确创建数组?

 Double[] Array1 = new Double[10];
    Array1 = new Double[] {1.0};  

If not then that would be why, that code below is not working for you. 如果不是,那就是为什么,下面的代码对您不起作用。 Not showing you the "good" word. 没有向您显示“好”字眼。

   if (Array1[0] == Array2[i])
    {

        System.out.println("good");

    }

... However, maybe if you change to 1.0 as value in the first variable... but its not the same to compare doubles.. its not recommended. ...但是,如果您将第一个变量的值更改为1.0 ...,但是比较双精度数是不一样的..不建议这样做。

Double Value = 1.0; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM