2014-12-04 2 views
-4

Моя программа продолжает складывать счет для каждого игрока, вместо того, чтобы держать его отдельно, например, если первый игрок получает 3/5, а второй получает 2/5, то оценка второго игрока будет 5. Я знаю, что ответ, вероятно, очень простой, но я не могу найти его в коде.Расчетный результат [Java процедурный]

Upfront спасибо!

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]

Смежные вопросы