java program using int and double

前端 未结 7 902
礼貌的吻别
礼貌的吻别 2020-12-21 00:16

I have written a simple Java program as shown here:

public class Test {

    public static void main(String[] args) {
        int i1 =2;
        int i2=5;
           


        
7条回答
  •  感情败类
    2020-12-21 00:36

    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

提交回复
热议问题