Java中的数字问题

来源:互联网 发布:美元指数软件 编辑:程序博客网 时间:2024/04/30 04:56

public class Volume {
    public static void main(String args[]) {
        double sunDiameter = 86500000d;//若数字不加d,则最后的结果不同,为什么?
        double sunRadius = sunDiameter / 2;
        double earthDiameter = 760000d;
        double earthRadius = earthDiameter / 2;
        double sunVolume;
        double earthVolume;
        double ratio;
        sunVolume = 4d / 3d * (Math.PI) * sunRadius * sunRadius * sunRadius;//若数字不加d,则最后的结果不同,为什么?
        earthVolume = 4d / 3d * (Math.PI) * earthRadius * earthRadius
                * earthRadius;
        ratio = sunVolume / earthVolume;
        System.out.println(sunDiameter);
        System.out.println(sunRadius);
        System.out.println(earthDiameter);
        System.out.println(earthRadius);
        System.out.println(sunVolume);
        System.out.println(earthVolume);
        System.out.println(ratio);
        System.out.println("The volume of the Earth is " + earthVolume
                + " cubic miles" + " and the volume of the Sun is " + sunVolume
                + " cubic miles"
                + " and the ratio of the volume of the Sun to "
                + "the volume of the Earth is " + ratio);

    }
}

原创粉丝点击