2014-12-04 76 views
-4

我的程序會爲每個玩家添加分數,而不是保持分開,例如,如果第一個玩家獲得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); 

回答

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]