Java arithmetics for calculating a percentage

后端 未结 8 785
一个人的身影
一个人的身影 2021-01-21 14:00

I have a little problem in my java application.

I have to calculate the score they have when they finish, I use this method:

public Float ScoreProcent(in         


        
8条回答
  •  长发绾君心
    2021-01-21 14:13

    Float num = (float) (100 / questions * correct); 
    

    It is executed in following way:

    Float num = (float) ((100 / questions) * correct); 
    

    since type of questions and correct is int, values after decimal are not conisdered.

    Now, 100 /questions = 100 / 38 = 2 and, num = (Float)(2 * 28) = 76.

    That's what you are getting.

提交回复
热议问题