为什么我从 getFloat() 得到的浮点值变成了 0.0?

Why has my float value from getFloat() become 0.0?

我是 java 的新手,我关于浮点数的编码有什么问题吗?因为它变成了 0.0 而我不太了解 float 你能解释一下吗?它发生在 getFloat

这个计算圆锥体积的程序

public class Cone_Volume
{
private int ri, hi, ivol;
private float rf, hf, fvol;

public Cone_Volume()
{

}

public Cone_Volume(int ri, int hi)
{
    this.ri = ri;
    this.hi = hi;
}

public Cone_Volume(float rf, float hf)
{
    this.rf = rf;
    this.hf = hf;
}

public int getInt()
{
    int ivol = (int)((3.142f*(ri*ri)*hi)/3);
    return ivol;
}

public float getFloat()
{
    float fvol = (float)((3.142f*(ri*ri)*hi)/3);
    return fvol;
}

public void Volume_I(int ri, int hi)
{
    this.ri = ri;
    this.hi = hi;
}

public void Volume_F(float rf, float hf)
{
    this.rf = rf;
    this.hf = hf;
}
}

此程序用于 Cone_Volume

的主要方法
public class TestConeVol
{
public static void main(String args[])
{
    Cone_Volume cone1 = new Cone_Volume (5,10);
    Cone_Volume cone2 = new Cone_Volume (3.5f,7.9f);
    Cone_Volume cone3 = new Cone_Volume ();
    Cone_Volume cone4 = new Cone_Volume ();

    cone3.Volume_I (10,30);
    cone4.Volume_F (9.4f,5.8f);

    System.out.println("Volume of cone one : " + cone1.getInt());
    System.out.println("Volume of cone two : " + cone2.getFloat());
    System.out.println("Volume of cone three : " + cone3.getInt());
    System.out.println("Volume of cone four : " + cone4.getFloat());
}
}

输出:

Volume of cone one : 261

Volume of cone two : 0.0

Volume of cone three : 3141

Volume of cone four : 0.0

在方法 getFloat 中,您错误地使用了 rihi 而不是 rfhf(分别)。

这里的重要一课是使用有意义的名称 ;)