I can not divide two numbers correctly

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

    score/numberOfQuestions will always provide a number between 0 and 1. You have two choices depending upon how accurate you need your computation. For most things, you can change the expression to (score * 100)/numberOfQUestions. This will give you two digits of accuracy. A problem would occur if score * 100 overflowed an int. Given your variable names, I doubt this would happen in this case. The second possibility would be to convert the expression to double for the computation.

提交回复
热议问题