我的程序会继续将每个玩家的分数相加,而不是将分数分开,例如,如果第一个玩家获得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。
在循环后为每个玩家添加以下内容:
score = 0;
或者,您可以直接在数组中增加分数。只是改变:
score = score + 1;
至:
playerscore[i] = playerscore[i] + 1;
或者简单地:
playerscore[i]++;
关于java - 计分系统和阵列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27302416/