在问答应用程序android的结果中显示正确答案(对于回答错误的问题)

在问答应用程序android的结果中显示正确答案(对于回答错误的问题),android,Android,我目前有一个测验应用程序,它由一个问题和三个选择题组成。在每个10项测验结束时,应用程序会显示所有10项问题的正确答案。下面是我目前实现的在结果页面中显示答案的代码 public static String getAnswers(List<Question> questions) { int question = 1; StringBuffer sb = new StringBuffer(); for (Question q : questions){

我目前有一个测验应用程序,它由一个问题和三个选择题组成。在每个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();
}
publicstaticstringgetanswers(列出问题){
int问题=1;
StringBuffer sb=新的StringBuffer();
关于(问题q:问题){
sb.append(“Q”)。append(问题)。append(“)”。append(Q.getQuestion())。append(“?\n”);
sb.append(“答案:”).append(q.getAnswer()).append(“\n\n”);
问题++;
}
使某人返回字符串();
}
我的问题Activity.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;
}
private void setQuestions(){
问题CTR++;
txtQNum.setText(“问题”+问题中心+”/10);
字符串question=Utility.capitalize(currentQ.getQuestion());
TextView qText=(TextView)findViewById(R.id.question);
qText.setText(问题);
列表答案=currentQ.getQuestionOptions();
TextView选项1=(TextView)findViewById(R.id.answer1);
选项1.setText(answers.get(0));
TextView选项2=(TextView)findViewById(R.id.answer2);
选项2.setText(answers.get(1));
TextView选项3=(TextView)findViewById(R.id.answer3);
选项3.setText(answers.get(2));
radioGroup.clearCheck();
}
公共void onClick(视图arg0){
如果(!checkAnswer())返回;
if(curquick.isGameOver()){
意图i=新意图(此,QuizResult.class);
星触觉(i);
完成();
}
否则{
currentQ=curquick.getNextQuestion();
设置问题();
}
}
私有布尔校验应答(){
字符串应答=getSelectedAnswer();
如果(答案==null){
返回false;
}
否则{
if(currentQ.getAnswer().equalsIgnoreCase(answer))
{
curquick.incrementRightAnswers();
}
否则{
curquick.incrementErrorAnswers();
}
返回true;
}
}
私有字符串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())
{
返回c1.getText().toString();
}
如果(c2.isChecked())
{
返回c2.getText().toString();
}
if(c3.isChecked())
{
返回c3.getText().toString();
}
返回null;
}

我想做的是只显示那些回答错误的问题的正确答案,这样就不会显示用户正确回答的不必要的问答。

您可以在
问题
类中添加一个布尔标志
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 ++;
    }
}
// ...

如果答案不正确,我应该在
//检查这里输入什么代码?很抱歉我真的不知道。我已经把你给我的密码放进去了@nem@KaitlinReyes嗨,我编辑了我的问题,让你更清楚一点,效果很好!谢谢大家!@nem@KaitlinReyes没问题:)