2011-12-06 4 views
0

내가 게시하려고하는 코드의 일부는 사용자에게 객관식 질문을 요청하는 프로그램에서 온 것입니다. 프로그램은 각 질문에 대한 점수를 유지하고 다른 배열에 저장 될 총계를가집니다. 나는 여러 플레이어가 이것을 연주 할 것이므로, 그래서 나는 2 개의 배열이 필요할 것이다.배열 요소의 합을 더하고 다른 배열에 저장 하시겠습니까? (Java)

//Asks questions and stores score in array 
public static int [] questions() 
{ 
    userinput=""; //input will be stored in here 
    int total[]= new int[100]; 
    int score[]=new int[5]; 
    for(int i=0; i < ps.length; i++) 
    { 
     userinput=JOptionPane.showInputDialog(que[i]); //Outputs a question stored in another array in another method. 
     if (response.equals(ans[i])) //this compares the user input to the correct answer of the question, which is in another method. 
     { 
      JOptionPane.showMessageDialog(null,"You selected " + " " + ans[i] + " You were correct, 1 point!"); 
      score[i]=1; 
      total[i]=total[i]+score[i]; 
     } 
     else if(!response.equals(ans[i])) // If the answer isn't correct 
     { 
      score[i]=0; // I want to assign 0 for the question 
      JOptionPane.showMessageDialog(null,"You're wrong!, The correct answer was "+ans[i]); 
     } 
    } // close loop 
    return total; // return's this to another method which will do all of the other work 
} 

내가 여기에 문제가있을 것 같다 : 답이 맞다면

JOptionPane.showMessageDialog(null,"You selected " + " " + ans[i] + " You were correct, 1 point!"); 
score[i]=1; 
total[i]=total[i]+score[i];  

내가 점수 []의 각 요소에 1을 추가하려면 여기

내가 무엇을 가지고 . 그럼 나는 총 점수 []를 축적하고 전체 []의 각 요소에 그것을 저장하고 싶습니다. 나는 그것을 배열에 저장하는 또 다른 메소드로 전체를 반환한다.

+1

의심스러운 코드는'totalscore [i] ++'와 같습니다. 게다가'totalscore'는 100 개의 elems를 가지지 만'score'는 15 elems를가집니다. 왜? –

+0

죄송합니다. 복사하여 붙여 넣기 오류입니다. 내가 작업하고있는 배열은 score []와 total []뿐입니다. – user1058452

+0

"total []"또는 "score []"이외의 배열이 있으면 무시하십시오! – user1058452

답변

2

좋아요, 그래야 현재 메서드의 현재 사용자 서수를 전달해야하므로 total 배열 내에서 올바른 위치를 계산할 수 있습니다. 당신이 여러 질문/응답 세션에서 총 점수를 집계 할 것 때문에, 당신은 외부에서 total 전달해야합니다 당신이 score 배열이 필요한 이유 초기 코드이기 때문에

public static void questions(int userOrdinal, int[] total) { 
    final int questionsPerUser = 5; 

    userinput = ""; //input will be stored in here 
    for (int i = 0; i < questionsPerUser; i++) { 
     userinput = JOptionPane.showInputDialog(que[i]); //Outputs a question stored in another array in another method. 
     if (response.equals(ans[i])) //this compares the user input to the correct answer of the question, which is in another method. 
     { 
      JOptionPane.showMessageDialog(null, "You selected " + " " + ans[i] + " You were correct, 1 point!"); 
      total[userOrdinal * questionsPerUser + i] = 1; 
     } else if (!response.equals(ans[i])) // If the answer isn't correct 
     { 
      total[userOrdinal * questionsPerUser + i] = 0; 
      JOptionPane.showMessageDialog(null, "You're wrong!, The correct answer was " + ans[i]); 
     } 
    } // close loop 
} 

미안 해요, 난 여전히 얻을 수 없다 total[i]++과 같고 score의 내용을 읽지 않으면 쓰기 만하십시오.

관련 문제