使用int和double的java程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16334630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java program using int and double
提问by chaitanya
I have written a simple Java program as shown here:
我编写了一个简单的 Java 程序,如下所示:
public class Test {
public static void main(String[] args) {
int i1 =2;
int i2=5;
double d = 3 + i1/i2 +2;
System.out.println(d);
}
}
Since variable d
is declared as double I am expecting the result of this program is 5.4
but I got the output as 5.0
由于变量d
被声明为 double 我期待这个程序的结果是5.4
但我得到的输出为5.0
Please help me in understanding this.
请帮助我理解这一点。
采纳答案by Maroun
i1/i2
will be 0. Since i1
and i2
are both integers.
i1/i2
将是 0。因为i1
和i2
都是整数。
If you have int1/int2
, if the answer is not a perfect integer, the digits after the decimal point will be removed. In your case, 2/5
is 0.4, so you'll get 0.
如果你有int1/int2
,如果答案不是一个完美的整数,小数点后的数字将被删除。在你的情况下,2/5
是 0.4,所以你会得到 0。
You can cast i1
or i2
to double
(the other will be implicitly converted)
您可以强制转换i1
或i2
到double
(另一个将被隐式转换)
double d = 3 + (double)i1/i2 +2;
double d = 3 + (double)i1/i2 +2;
回答by Vineet Singla
i1/i2
when converted to int gives 0. ie. why you are getting 5.0. Try this :
i1/i2
当转换为 int 时给出 0。即。为什么你得到 5.0。尝试这个 :
public static void main(String args[])
{
int i1 =2;
int i2=5;
double d = 3 + (double)i1/(double)i2 +2;
System.out.println(d);
}
回答by Richard Tingle
This line is done in parts:
这行是分部分完成的:
double d = 3 + i1/i2 +2;
double d = 3 + (i1/i2) +2;
double d = 3 + ((int)2/(int)3) +2;
double d = 3 + ((int)0) +2;
double d = (int)5;
double d = 5;
The double just means that the answer will be cast to a double, it doesn't have any effect till the answer is computed. You should write
double 只是意味着答案将被转换为 double,在计算答案之前它没有任何影响。你应该写
double d = 3d + (double)i1/i2 +2d; //having one double in each "part" of the calculation will force it to use double maths, 3d and 2d are optional
回答by Madhusudan Joshi
回答by NullPointer
i1/i2
will be 0 because both i1
and 12
are integers.
i1/i2
将是 0,因为i1
和12
都是整数。
if you cast i1
or i2
to double
then it will give the desired output.
如果您投射i1
或i2
到double
那么它将提供所需的输出。
double d = 3 + (double)i1/i2 +2;
回答by Amit Ranjan
int i1 =2;
int i2=5;
double d = 3 + (double)i1/(double)i2 +2;
if i1/i2 will be fractional value then double will help it to be in fraction instead of int. so now you will the result as you want. or you can also use following code double d = 3+(double)i1/i2+2; In this line i1 is converted into double which will be divided with i2 and result will be in double, so again result will be as 5.4
如果 i1/i2 是小数值,那么 double 将帮助它成为小数而不是整数。所以现在你将得到你想要的结果。或者您也可以使用以下代码 double d = 3+(double)i1/i2+2; 在这一行中,i1 被转换为 double 将与 i2 相除,结果将变为 double,因此结果将再次变为 5.4
回答by Parmanand
Since i1=2 and i2=5 are integer type and when you divide (2/5) them, It gives integer value (0) because fractional part(.4) get discarded. So put (double)i1/i2 on the equation.
由于 i1=2 和 i2=5 是整数类型,当你将它们除以 (2/5) 时,它给出整数值 (0),因为小数部分 (.4) 被丢弃。所以把 (double)i1/i2 放在等式上。