测验应用程序android的结果(错误答案问题)中显示正确的答案

时间:2014-09-20 17:01:26

标签: android

我目前有一个测验应用程序,其中包含一个问题&三个选择。在每10项测验结束时,应用程序会显示所有10项问题的正确答案。这是我目前在结果页面中显示答案的代码

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&amp; A得到了正确答案。

1 个答案:

答案 0 :(得分:1)

您可以在isAnswerCorrect类中添加布尔标记Question。默认情况下将其设置为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;
    }
}

您在if方法的checkAnswer()声明中将答案设置为正确:

// ...
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 ++;
    }
}
// ...