2014-12-04 95 views
0

我的程序會爲每個玩家添加分數,而不是將其分開,例如,如果第一個玩家獲得3/5,第二個玩家獲得2/5,則第二個玩家的分數顯示爲5。我知道答案可能很簡單,但我無法在代碼中找到它。評分系統和陣列

public static void questions(String[] question, String[] answer, int n) { 

    String[] name = new String[n]; // Player Names 
    int[] playerscore = new int[n]; // Argument for Score 
    String[] que = new String[question.length]; //Questions for Loops 
    int score = 0; // Declare the score 

    /* --------------------------- For loop for number of players --------------------------- */ 
    for (int i = 0; i < n; i++) { 
     name[i] = JOptionPane.showInputDialog("What is your name player" + (i + 1) + "?"); 
     JOptionPane.showMessageDialog(null, "Hello :" + name[i] + " Player number " + (i + 1) + ". I hope your ready to start!"); 

     /* --------------------------- Loop in Loop for questions --------------------------- */ 
     for (int x = 0; x < question.length; x++) { 
      que[x] = JOptionPane.showInputDialog(question[x]); 

      if (que[x].equals(answer[x])) { 
       score = score + 1; 
      } else { 
       JOptionPane.showMessageDialog(null, "Wrong!"); 
      } 

     } // End for loop for Question 
     playerscore[i] = score; 
     System.out.println("\nPlayer" + (i) + "Name:" + name[i] + "\tScore" + score); 
    } 
} 
+0

你有一個單一的得分計數器。你需要爲每個玩家提供一個不同的計數器,例如得分數組。 – 2014-12-04 19:33:17

回答

3

您需要在每位玩家開始之前將分數重置爲0。

循環每個球員後補充一點:

score = 0; 

另外,您可以在陣列中直接增加了比分。只要改變:

score = score + 1; 

到:

playerscore[i] = playerscore[i] + 1; 

或者乾脆:

playerscore[i]++; 
0

分配行

playerscore[i] = score; 

每每一位玩家被分配後,他內循環得分。因爲分數被聲明爲實例變量,所以它不會區分兩個不同的玩家。爲了讓每個玩家都擁有自己的分數,只要問題超出,就將分數設爲零。