2014-09-20 3 views
1

현재 객관식 질문이 & 인 퀴즈 앱이 있습니다. 10 개 항목의 퀴즈가 끝나면 모든 10 가지 항목에 대해 올바른 답변이 표시됩니다. 저는 여기에 현재 결과 페이지퀴즈 app android의 결과에 올바른 답을 표시합니다.

public static String getAnswers(List<Question> questions) { 
    int question = 1; 
    StringBuffer sb = new StringBuffer(); 

    for (Question q : questions){ 
     sb.append("Q").append(question).append(") ").append(q.getQuestion()).append("? \n"); 
     sb.append("Answer: ").append(q.getAnswer()).append("\n\n"); 
     question ++; 
    } 

    return sb.toString(); 
} 

에 답을 표시하기 위해 구현하는 코드는 그리고 난 내가 만 표시되고 싶었던 무엇 내 QuestionActivity.java

private void setQuestions() { 
    questionCtr++; 
    txtQNum.setText("Question " + questionCtr + "/10"); 

    String question = Utility.capitalise(currentQ.getQuestion()); 
    TextView qText = (TextView) findViewById(R.id.question); 
    qText.setText(question); 

    List<String> answers = currentQ.getQuestionOptions(); 
    TextView option1 = (TextView) findViewById(R.id.answer1); 
    option1.setText(answers.get(0)); 

    TextView option2 = (TextView) findViewById(R.id.answer2); 
    option2.setText(answers.get(1)); 

    TextView option3 = (TextView) findViewById(R.id.answer3); 
    option3.setText(answers.get(2)); 

    radioGroup.clearCheck(); 
} 


public void onClick(View arg0) { 
    if (!checkAnswer()) return; 

    if (curQuiz.isGameOver()){ 
     Intent i = new Intent(this, QuizResult.class); 
     startActivity(i); 
     finish(); 

    } 
    else{ 
     currentQ = curQuiz.getNextQuestion(); 
     setQuestions(); 
    } 
} 

private boolean checkAnswer() { 
    String answer = getSelectedAnswer(); 

    if (answer==null){ 

     return false; 
    } 
    else { 

     if (currentQ.getAnswer().equalsIgnoreCase(answer)) 
     { 

      curQuiz.incrementRightAnswers(); 
     } 
     else{ 

      curQuiz.incrementWrongAnswers(); 
     } 
     return true; 
    } 
} 

private String getSelectedAnswer() { 
    RadioButton c1 = (RadioButton)findViewById(R.id.answer1); 
    RadioButton c2 = (RadioButton)findViewById(R.id.answer2); 
    RadioButton c3 = (RadioButton)findViewById(R.id.answer3); 

    if (c1.isChecked()) 
    { 
     return c1.getText().toString(); 
    } 
    if (c2.isChecked()) 
    { 
     return c2.getText().toString(); 
    } 
    if (c3.isChecked()) 
    { 
     return c3.getText().toString(); 
    } 
    return null; 
} 

에 이러한이 대답 된 질문에 대한 정답은 입니다.이므로 사용자가 올바르게 대답 한 불필요한 Q & A가 표시되지 않습니다.

답변

1

Question 클래스 내에 부울 플래그 isAnswerCorrect을 추가 할 수 있습니다. 기본적으로 false으로 설정하고 사용자가 질문에 대한 올바른 대답을 추측 할 때마다 그 플래그를 true으로 만듭니다.

:

// ... 
if (currentQ.getAnswer().equalsIgnoreCase(answer)) 
{ 
    curQuiz.incrementRightAnswers(); 
    currentQ.setAnsweredCorrectly(); // set the answer as correct here (boolean flag becomes true) 
} 
// ... 

그런 getAnswers() 내부 루프에서, 단지 제대로 응답하지 않은 답변을 추가합니다

class Question { 
    // ... other fields you already have here 
    boolean isAnswerCorrect = false; // boolean flag for correct answer initialized to false 

    // ... constructor, getters, setters 

    public void setAnsweredCorrectly() { // you use this method to set the answer to correct 
     isAnswerCorrect = true; 
    } 

    public boolean isAnsweredCorrectly() { // you will use this method to only get correct answers 
     return isAnswerCorrect; 
    } 
} 

당신은 당신의 checkAnswer() 방법의 if 문 안쪽에 알맞은 답을 설정

// ... 
for (Question q : questions){ 
    if(!q.isAnsweredCorrectly()) { // check here if the answer wasn't correct and append it 
     sb.append("Q").append(question).append(") ").append(q.getQuestion()).append("? \n"); 
     sb.append("Answer: ").append(q.getAnswer()).append("\n\n"); 
     question ++; 
    } 
} 
// ... 
+0

'답변이 올바르지 않으면'// 여기에 체크 할 때 어떤 코드를 입력해야합니까? ' 죄송합니다. 정말로 모른다. 나는 이미 주어진 코드를 넣었습니다. @nem –

+0

@KaitlinReyes 안녕하세요, 제 질문을 편집하여 일을 좀 더 명확하게했습니다. – nem035

+0

매우 잘 작동합니다! 고맙습니다! @nem –

관련 문제