我试图从for循环中的数组值中获取一个百分比。
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 + "%!");
我做错了什么? playerScore
显示一个值,但当我尝试使用下面的计算时,无论如何都会显示0.0。
示例运行:
John got 3 questions right out of 10!
Which is 0.0%!
答案 0 :(得分:2)
尝试在分割前投射值:
double playerScorePercentage = ((double)playerScore[i] / (double)question.length) * 100;
如果您不知道该主题,请在网上搜索“整数除法”。