Android Studio实现多选题APP题目与对应选项同步随机化方案
Android多选题APP随机题目同步选项与答案解决方案
当前APP的问题是随机题目时,仅单独打乱了question数组,导致题目索引和对应的choices、correctAnswer数组索引错位,无法同步显示对应选项和正确答案。以下是具体解决方法:
1. 重构数据结构,封装题目实体类
把题目、选项、正确答案封装成一个Question类,确保三者绑定,从根源上避免索引错位:
public class Question { private String questionText; private String[] choices; private String correctAnswer; public Question(String questionText, String[] choices, String correctAnswer) { this.questionText = questionText; this.choices = choices; this.correctAnswer = correctAnswer; } // Getter方法 public String getQuestionText() { return questionText; } public String[] getChoices() { return choices; } public String getCorrectAnswer() { return correctAnswer; } }
修改QuestionAnswer类,用List存储Question对象:
import java.util.ArrayList; import java.util.List; public class QuestionAnswer { public static List<Question> getQuestions() { List<Question> questions = new ArrayList<>(); questions.add(new Question( "Who invented C++ ?", new String[]{"Dennis Retchie","Ken Thompson","Brian Kernighan","Bjarne Stroustrup"}, "Bjarne Stroustrup" )); questions.add(new Question( "Which is not the programming language ?", new String[]{"Java","C++","Notepad++","Python"}, "Notepad++" )); questions.add(new Question( "What is C++ ?", new String[]{"C++ is an object oriented programming language", "C++ is a procedural programming language", "C++ supports both procedural and object oriented programming language", "C++ is a functional programming language"}, "C++ supports both procedural and object oriented programming language" )); questions.add(new Question( "Which of the following is the correct syntax of including a user defined header files in C++ ?", new String[]{"#include [userdefined]","#include <userdefined.h>","#include “userdefined”","#include <userdefined>"}, "#include “userdefined”" )); return questions; } }
2. 修改Quiz类的逻辑适配新结构
调整变量和随机逻辑,确保题目与选项、答案同步:
import android.app.AlertDialog; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.Collections; import java.util.List; public class Quiz extends AppCompatActivity implements View.OnClickListener { TextView totalQuestion_tv, question_tv,score_tv; Button ansA, ansB, ansC, ansD, submitBtn,stopBtn; int score; List<Question> questionList; int totalQuestion; int currentQuestionIndex = 0; String selectedAnswer = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz); getSupportActionBar().setDisplayHomeAsUpEnabled(false); totalQuestion_tv = findViewById(R.id.total_question); question_tv = findViewById(R.id.question); score_tv = findViewById(R.id.my_score); ansA = findViewById(R.id.ans_A); ansB = findViewById(R.id.ans_B); ansC = findViewById(R.id.ans_C); ansD = findViewById(R.id.ans_D); submitBtn = findViewById(R.id.submit_btn); stopBtn = findViewById(R.id.stop_btn); ansA.setOnClickListener(this); ansB.setOnClickListener(this); ansC.setOnClickListener(this); ansD.setOnClickListener(this); submitBtn.setOnClickListener(this); stopBtn.setOnClickListener(this); stopBtn.setBackgroundColor(Color.RED); // 初始化题目列表并随机打乱 questionList = QuestionAnswer.getQuestions(); totalQuestion = questionList.size(); randomizeQuestions(); totalQuestion_tv.setText("Total Question: "+totalQuestion); loadNewQuestion(); } @Override public void onClick(View v) { ansA.setBackgroundColor(Color.WHITE); ansB.setBackgroundColor(Color.WHITE); ansC.setBackgroundColor(Color.WHITE); ansD.setBackgroundColor(Color.WHITE); Button click_btn = (Button) v; if(click_btn.getId()==R.id.submit_btn){ if(selectedAnswer.equals(questionList.get(currentQuestionIndex).getCorrectAnswer())) { score++; } score_tv.setText("Score: "+score); currentQuestionIndex++; loadNewQuestion(); } else if(click_btn.getId() == R.id.stop_btn){ new AlertDialog.Builder(this) .setTitle(" Quit Quiz ") .setMessage("Are you sure you want to quit ?") .setPositiveButton("Quit",((dialogInterface, x) -> finishQuiz())) .setNegativeButton("Cancel",((dialogInterface, x) -> {})) .setCancelable(false) .show(); } else{ selectedAnswer = click_btn.getText().toString(); click_btn.setBackgroundColor(Color.CYAN); } } private void loadNewQuestion() { if(currentQuestionIndex==totalQuestion){ finishQuiz(); return; } Question currentQuestion = questionList.get(currentQuestionIndex); question_tv.setText(currentQuestion.getQuestionText()); String[] choices = currentQuestion.getChoices(); ansA.setText(choices[0]); ansB.setText(choices[1]); ansC.setText(choices[2]); ansD.setText(choices[3]); } // 随机打乱整个题目列表,确保题目与选项、答案同步 void randomizeQuestions(){ Collections.shuffle(questionList); } void finishQuiz() { String passStatus = ""; if(score > totalQuestion*0.60){ passStatus = "Passed"; }else{ passStatus = "Failed"; } int finalScore = score; score = 0; new AlertDialog.Builder(this) .setTitle(passStatus) .setMessage("Score is: "+finalScore+" out of "+ totalQuestion) .setPositiveButton("Restart",((dialogInterface, i) -> restartQuiz())) .setNegativeButton("Exit",((dialogInterface, i) -> finish())) .setCancelable(false) .show(); } void restartQuiz() { score = 0; currentQuestionIndex = 0; randomizeQuestions(); loadNewQuestion(); } }
关键修改说明
- 实体类封装:将题目、选项、正确答案绑定为一个对象,彻底避免索引错位问题
- 随机逻辑调整:不再单独打乱题目数组,而是打乱整个
Question对象列表,确保每个题目对应的选项和答案始终同步 - 修复分数显示bug:原代码在弹窗显示前重置了分数,导致显示0分,现在先保存最终分数再重置
- 优化取消按钮逻辑:点击取消时直接关闭弹窗,无需重复加载题目
内容的提问来源于stack exchange,提问作者J i N A D A PH
相关产品推荐
相关产品推荐

