Java arithmetics for calculating a percentage

后端 未结 8 694
一个人的身影
一个人的身影 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:16

    Float num = (float) (100 / y * x);
    

    The problem is that you weren't using the float you just built before.

    Alternatively I suggest this :

    public float getScorePercent(int questions, int correct){
        return 100f * correct / questions;
    }
    

    It's generally advised to avoid Float and use float, except when you really need objects (that is rarely).

提交回复
热议问题