Dividing two integers in Java gives me 0 or 100?

后端 未结 5 985
悲&欢浪女
悲&欢浪女 2020-12-01 20:52

I\'m trying to divide two integers and multiply by 100 but it keeps giving only 0 or 100. Can someone help me?

    int x= (a/b)*100;

if a w

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 21:30

    This sounds like you are not correctly typing your variables; two integer divisions result in an integer, not a float or double. For example:

    (int)3 / (int)5 = 0
    (float)3 / (float)5 = 0.6
    

提交回复
热议问题