-4

我的程序不断将每个玩家的分数相加,而不是将其分开,例如,如果第一个玩家获得 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);
4

1 回答 1

0

重置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]

于 2014-12-04T17:50:33.380 回答