2014-09-20 65 views
1

我目前有一個測驗應用程序,其中包括一個問題&三個多項選擇。在每10項測驗結束時,該應用程序將顯示所有10項問題的正確答案。下面是我目前實施,以顯示在結果頁面顯示正確答案的結果(對於錯誤回答問題)的測驗應用程序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 &用戶已正確回答的答案。

回答

1

您可以在Question類中添加布爾標誌isAnswerCorrect。默認情況下將其設置爲false,並且每當用戶猜出正確的答案時,您就會發出該標記爲true的問題。

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語句中正確的是:

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

然後在你的循環內getAnswers(),只是追加了未正確回答的答案:

// ... 
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 – 2014-09-22 13:00:21

+0

@KaitlinReyes嗨,我編輯了我的問題,讓事情變得更加清晰 – nem035 2014-09-22 13:59:36

+0

工作得很好!謝謝! @nem – 2014-09-22 14:25:07

相關問題