为什么Java这个计算不对?

Why doesn't Java calculate this calculation right?

我最近开始学习Java。我开始研究一个简单的数学项目,用 (1 + 1 / x)^x 来近似欧拉数。但是在第一步,程序 return 输入了错误的值。这是我的最小化代码,解决方案是错误的。

public class MyClass
{
    public static void main(String[] args)
    {
        System.out.println(1 + 1 / 2);
    }
}

程序returns 1. 结果在我看来很矛盾。 1 + 1 / 2 return 1 怎么可能?我期待 1.5.

我尝试了一些调试:

double x = 1 + Math.pow(2, -1);
System.out.println(x);

现在可以正常使用了。怎么会这样?

我正在对两个整数进行除法。我应该在两个双打或花车上做这个:

System.out.println(1.0 + 1.0 / 2.0);

因为我们可以缩短双打 1.0 -> 1.,我们可以缩短这个:

System.out.println(1.+1./2.);

感谢您的回答! (特别是评论中的Naman, njzk2 and FredK。)