2014-09-20 71 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

相关问题