Java arithmetics for calculating a percentage

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

    Try this:

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

    or

    num = (correct * 100.0) / questions;
    
    0 讨论(0)
  • 2021-01-21 14:28

    There are no unreasonable quesions.

    You are calculating what fraction of correct are there among the questions. The mathematical way of saying this is:

      correct/questions
    

    Now, since they can go onto decimals you need to use that Floats you mentioned in your code. So:

     (float)correct/(float)quesions  
    

    That is how you cast something to float. To get the answer in percentage(in 100) it must be multiplied by 100 which is

      100 * (float)correct/(float)quesions 
    

    And there you go :)

    For proper way to get it coded listen to other answers.

    0 讨论(0)
提交回复
热议问题