2014-12-02 79 views
0

我想從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%! 

回答

2

嘗試分割之前鑄造值:

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

而且在網絡上搜索「整數除法」,如果你不知道的話題。