首页 文章

当我们为double或float指定一个值(<1)而没有后缀'd'或'f'时,为什么输出为0.0? [重复]

提问于
浏览
-2

这个问题在这里已有答案:

当我将一些值(<1)分配给float或double而没有任何后缀'f'或'd'时,为什么输出显示为0.0?我的节目是

public class Example {

    double a = 1/2d;
    float b = 1/2f;
    double c = 1/2;
    float d = 1/2;

    public static void main(String[] args) {
        Example e = new Example();
        System.out.println("a: "+e.a);
        System.out.println("b: "+e.b);
        System.out.println("c: "+e.c);
        System.out.println("d: "+e.d);
    }
}

输出是

a: 0.5
  b: 0.5
  c: 0.0
  d: 0.0

1 回答

相关问题