2014-12-04 94 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; 

每每一位玩家被分配后,他内循环得分。因为分数被声明为实例变量,所以它不会区分两个不同的玩家。为了让每个玩家都拥有自己的分数,只要问题超出,就将分数设为零。