计算分数[Java程序]

时间:2014-12-04 17:44:32

标签: java arrays procedural

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

1 个答案:

答案 0 :(得分: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]