Java – I can’t divide two numbers correctly

int percent = (score/numberOfQuestions)*100;
int percent = (score/numberOfQuestions)*100;
progressText.setText(score+"/"+numberOfQuestions+"  "+percent+"%");

Return 0%, no matter I'm tired I try to convert it to int, double, float

Why does it return a number of 0%, such as score = 5, numberofquestions = 8?

Solution

The problem is to split two integers to give the integer part of the result Therefore, (score / numberofquestions) will always be 0

int percent = (score * 100 / numberOfQuestions);

Then * 100 will be executed first, and the division will give you the correct result

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>