I can not divide two numbers correctly

后端 未结 6 611
情深已故
情深已故 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:17

    You need to cast on either of them: -

    float percent = ((float)score/numberOfQuestions)*100;
    

    Since, 5 / 10 is already 0.. Casting the final result to any type will give you 0 only, as in below code: -

    float percent = ((float)(score/numberOfQuestions))*100;
    

    This will also give you 0.0. Since you are casting 0 to float. Doesn't matter..

提交回复
热议问题