想要在Android测验游戏结束时显示正确的答案和玩家选择的答案

时间:2015-10-03 09:00:05

标签: 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();}

我的QuesActivity是:

private void setQuestions() {
    //set the question text from current question
    String question = Utility.capitalise(currentQ.getQuestion()) + "?";
    TextView qText = (TextView) findViewById(R.id.question);
    qText.setText(question);

    //set the available options
    List<String> answers = currentQ.getQuestionOptions();
    btn1 = (Button) findViewById(R.id.button1);
    btn1.setText(Utility.capitalise(answers.get(0)));
    btn1.setOnClickListener(this);

    btn2 = (Button) findViewById(R.id.button2);
    btn2.setText(Utility.capitalise(answers.get(1)));
    btn2.setOnClickListener(this);

    btn3 = (Button) findViewById(R.id.button3);
    btn3.setText(Utility.capitalise(answers.get(2)));
    btn3.setOnClickListener(this);

    btn4 = (Button) findViewById(R.id.button4);
    btn4.setText(Utility.capitalise(answers.get(3)));
    btn4.setOnClickListener(this);
}





@Override
public void onClick(View arg0) {


    if (!checkAnswer()) return;

    if (currentGame.isGameOver()){
        Log.d("Questions", "End of game! lets add up the scores..");
        Log.d("Questions", "Questions Correct: " + currentGame.getRight());
        Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
        Intent i = new Intent(this, EndgameActivity.class);
        startActivity(i);
        finish();
    }
    else{
        Intent i = new Intent(this, QuestionActivity.class);
        startActivity(i);
        finish();
    }
}





private boolean checkAnswer() {
    String answer = getSelectedAnswer();
    Log.d("yourans", currentQ.getAnswer()+" "+answer);

        if (currentQ.getAnswer().equalsIgnoreCase(answer))
        {   
            Log.d("Questions", "Correct Answer!");
            currentGame.incrementRightAnswers();
        }
        else{
            Log.d("Questions", "Incorrect Answer!");
            currentGame.incrementWrongAnswers();
        }
        return true;
    }


private String getSelectedAnswer() {
    Button b1 = (Button)findViewById(R.id.button1);
    Button b2 = (Button)findViewById(R.id.button2);
    Button b3 = (Button)findViewById(R.id.button3);
    Button b4 = (Button)findViewById(R.id.button4);
    if (b1.isPressed())
    {
        return b1.getText().toString();
    }
    if (b2.isPressed())
    {
        return b2.getText().toString();
    }
    if (b3.isPressed())
    {
        return b3.getText().toString();
    }
    if (b4.isPressed())
    {
        return b4.getText().toString();
    }

    return null;
}}

1 个答案:

答案 0 :(得分:2)

在类问题中添加另一个名为userAnswer的属性。一个字段用于正确答案,一个用于用户答案。将有另一个问题是正确的。 你有一个名为Question的课程。就像那样,我忽略了语法。

class Question { 
String mQuestion; 
String mOption1; 
String mOption2; 
String mOption3; 
String mOption4; 
String mCorrectAnswer; 
String mUserAnswer; 
String mStatusOfQuestion (Asked/Not Asked Yet) 
}

您将以您现在正在进行的方式设置问题。但是也要设置类对象并将它们放在arrayList中。

ArrayList<Question> mQuestionsList = new ArrayList<Question>
setQuestion() {
Question mQuestion = new Question();
String question = Utility.capitalise(currentQ.getQuestion()) + "?";
    TextView qText = (TextView) findViewById(R.id.question);
    qText.setText(question);
mQuestion.setQuestion(question);
    //set the available options
    List<String> answers = currentQ.getQuestionOptions();
    btn1 = (Button) findViewById(R.id.button1);
    btn1.setText(Utility.capitalise(answers.get(0)));
mQuestion.setOption1(answers.get(0));
    btn1.setOnClickListener(this);

    btn2 = (Button) findViewById(R.id.button2);
    btn2.setText(Utility.capitalise(answers.get(1)));
mQuestion.setOption2(answers.get(1));
    btn2.setOnClickListener(this);

    btn3 = (Button) findViewById(R.id.button3);
    btn3.setText(Utility.capitalise(answers.get(2)));
    btn3.setOnClickListener(this);
mQuestion.setOption3(answers.get(2));

    btn4 = (Button) findViewById(R.id.button4);
    btn4.setText(Utility.capitalise(answers.get(3)));
    btn4.setOnClickListener(this);
mQuestion.setOption4(answers.get(3));
mQuestion.setCorrectAnswer(corectAnswer);
}

当用户点击某个选项时,设置问题对象的值,如

onUserclick(int position 2) {
   mQuestionsList.get(position).setUserAnswer(tappedOption);
   if(tappedOption == mQuestionsList.get(position).getCorrectAnswer())
      isCorrect = true;
   else
      isCorrect = false;
}