gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:39:50 26 4
gpt4 key购买 nike

我目前有一个测验应用程序,其中包含一个问题和三个多项选择。在每 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;
}

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

最佳答案

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

关于android - 在测验应用程序 android 的结果(错误答案问题)中显示正确答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com