Reputation: 33
I am trying to get a percentage from an array value in a for-loop.
System.out.println(playerName[i+1] + " got " + playerScore[i] + " questions right out of " + question.length + "!\n");
double playerScorePercentage = (playerScore[i] / question.length) * 100;
System.out.println("Which is " + playerScorePercentage + "%!");
What am I doing wrong? The playerScore
displays a value but when I am trying to do a calculation with it below, it displays 0.0 no matter what.
An example run:
John got 3 questions right out of 10!
Which is 0.0%!
Upvotes: 0
Views: 516
Reputation: 4065
Try casting the values before the division:
double playerScorePercentage = ((double)playerScore[i] / (double)question.length) * 100;
And search the web for "integer division" if you don't know the topic.
Upvotes: 2