如何从数组值中获取百分比

时间:2014-12-02 22:29:09

标签: java arrays

我试图从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%!

1 个答案:

答案 0 :(得分:2)

尝试在分割前投射值:

double playerScorePercentage = ((double)playerScore[i] / (double)question.length) * 100;

如果您不知道该主题,请在网上搜索“整数除法”。