Division in Java always results in zero (0)? [duplicate]

*爱你&永不变心* 提交于 2019-11-26 10:36:26

You're doing integer division.

You need to cast one operand to double.

Cacho Santa

You are doing an integer division, cast the values to float and change the datatype of the variable bmi to float.

Like this:

float bmi = (float)weight/(float)(height*height);

You should also change the return type of your method public int computeBMI() to float.

I recommend you to read this stackoverflow question.

Here you have a list of the Primitive Data Types in Java with its full description.

Hope it helps!

Because bmi is an integer. Either declare bmi or Weight, Height as floating point numbers. When you use integers in a division, you will get integer division. When you use doubles/floats, you will get floating point division

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!