使用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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:22:18  来源:igfitidea点击:

java program using int and double

javaintdouble

提问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 dis declared as double I am expecting the result of this program is 5.4but I got the output as 5.0

由于变量d被声明为 double 我期待这个程序的结果是5.4但我得到的输出为5.0

Please help me in understanding this.

请帮助我理解这一点。

采纳答案by Maroun

i1/i2will be 0. Since i1and i2are both integers.

i1/i2将是 0。因为i1i2都是整数。

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/5is 0.4, so you'll get 0.

如果你有int1/int2,如果答案不是一个完美的整数,小数点后的数字将被删除。在你的情况下,2/5是 0.4,所以你会得到 0。

You can cast i1or i2to double(the other will be implicitly converted)

您可以强制转换i1i2double(另一个将被隐式转换)

double d = 3 + (double)i1/i2 +2;

double d = 3 + (double)i1/i2 +2;

回答by Vineet Singla

i1/i2when 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

This linkprovides information about data type conversion, both implicit and explicit type.

此链接提供有关数据类型转换的信息,包括隐式和显式类型。

To provide exact answer to the question will be :

提供问题的确切答案将是:

double d = 3 + (double)i1/i2 + 2

回答by NullPointer

i1/i2will be 0 because both i1and 12are integers.

i1/i2将是 0,因为i112都是整数。

if you cast i1or i2to doublethen it will give the desired output.

如果您投射i1i2double那么它将提供所需的输出。

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 放在等式上。