Java 中的简单划分 - 这是错误还是功能? [英] Simple division in Java - is this a bug or a feature?
本文介绍了Java 中的简单划分 - 这是错误还是功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 Java 应用程序中尝试这个简单的计算:
I'm trying this simple calculation in a Java application:
System.out.println("b=" + (1 - 7 / 10));
显然我希望输出为 b=0.3
,但实际上我得到的是 b=1
.
Obviously I expect the output to be b=0.3
, but I actually get b=1
instead.
什么?!为什么会发生这种情况?
What?! Why does this happen?
如果我写:
System.out.println("b=" + (1 - 0.7));
我得到了正确的结果,即 b=0.3
.
I get the right result, which is b=0.3
.
这里出了什么问题?
推荐答案
您正在使用整数除法.
试试7.0/10
.
这篇关于Java 中的简单划分 - 这是错误还是功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文