计算分数[Java程序]
问题描述:
我的程序会为每个玩家添加分数,而不是保持分开,例如,如果第一个玩家获得3/5,第二个玩家获得2/5,第二个玩家的分数显示将会是5.我知道答案可能非常简单,但我无法在代码中找到它。计算分数[Java程序]
前期感谢!
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);
答
重置score
变量的循环中,然后将其放置在playerscore
相应的元素。下面的代码:
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!");
score = 0; //reset the score variable
/* --------------------------- 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;
System.out.println("\nPlayer"+(i)+ "Name:"+name[i]+"\tScore"+score);
}
else{
JOptionPane.showMessageDialog(null,"Wrong!");
}
} // End for loop for Question
playerscore[i] = score; //assign the score for each player
}
然后,每当你想要的分数name[i]
你可以只打印playerscore[i]