我的程序不断累加每个玩家的分数,而不是将其分开,例如,如果第一个玩家获得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 :(得分:4)
您需要在每位玩家开始前将分数重置为0.
在每个玩家的循环之后添加:
score = 0;
或者,您可以直接在数组中增加分数。只需改变:
score = score + 1;
为:
playerscore[i] = playerscore[i] + 1;
或简单地说:
playerscore[i]++;
答案 1 :(得分:0)
在
行后分配score=0
playerscore[i] = score;
每个玩家都会在内圈中分配他的分数。由于得分被声明为实例变量,因此不会区分两个不同的玩家。为了让每个玩家拥有自己的分数,一旦问题超过,就将分数归零。