I can not divide two numbers correctly

后端 未结 6 507
情深已故
情深已故 2021-01-17 17:44
int percent = (score/numberOfQuestions)*100;
progressText.setText(score+\"/\"+numberOfQuestions+\"  \"+percent+\"%\");

returns 0% no matter what I

6条回答
  •  野的像风
    2021-01-17 18:16

    The problem is that divide two integers gives you the integer part of the result. So, (score/numberOfQuestions) will be always 0.
    What you should do is

    int percent = (score * 100 / numberOfQuestions);
    

    Then the *100 will be executed first, then the divide will give you correct result.

提交回复
热议问题