-4

私のプログラムは、たとえば、最初のプレーヤーが 3/5 を取得し、2 番目のプレーヤーが 2/5 を取得した場合、2 番目のプレーヤーのスコア表示は 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 に答える