You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio开发问答游戏:如何避免重复出题及优化选题逻辑

Hey there! Let's work through your quiz game requirements—since you're using a standalone Java class for your 5000-question bank instead of SQLite, we can solve both of your problems with clean, efficient logic. Here's how:

1. Optimize Question Selection to Avoid Repeating Previously Used Questions

The core idea here is to track which questions have already been used, so we only pull from a pool of unused ones for each new game. We can use two separate lists to manage this:

  • A pool of available (unused) questions
  • A list of already used questions

Here's a practical implementation in your question bank class:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class QuizBank {
    // Your Question model class (adjust fields to match your needs)
    public static class Question {
        private String questionText;
        private String[] options;
        private int correctAnswerIndex;

        public Question(String questionText, String[] options, int correctAnswerIndex) {
            this.questionText = questionText;
            this.options = options;
            this.correctAnswerIndex = correctAnswerIndex;
        }

        // Add getters and setters for your fields here
    }

    private final List<Question> allQuestions;
    private List<Question> availableQuestions;
    private final List<Question> usedQuestions;
    private final Random random;

    public QuizBank() {
        // Initialize your full 5000-question list here
        allQuestions = new ArrayList<>();
        populateAllQuestions(); // Your custom method to add all 5000 questions

        usedQuestions = new ArrayList<>();
        random = new Random();
        // We'll initialize availableQuestions after first-time shuffling
    }

    // Call this to set up the initial available question pool
    public void initializeAvailablePool() {
        availableQuestions = new ArrayList<>(allQuestions);
    }

    public Question getNextQuestion() {
        if (availableQuestions.isEmpty()) {
            // All questions have been used—either reset the pool or notify the user
            resetQuestionPool();
            // Optional: Return null or a placeholder to indicate no new questions
            // return null;
        }

        // Pick a random unused question
        int randomIndex = random.nextInt(availableQuestions.size());
        Question selectedQuestion = availableQuestions.get(randomIndex);

        // Move the selected question from available to used
        availableQuestions.remove(randomIndex);
        usedQuestions.add(selectedQuestion);

        return selectedQuestion;
    }

    // Reset the pool by moving all used questions back to available
    public void resetQuestionPool() {
        availableQuestions.addAll(usedQuestions);
        usedQuestions.clear();
        // Optional: Reshuffle the pool when resetting (if you want fresh order)
        // Collections.shuffle(availableQuestions, random);
    }

    private void populateAllQuestions() {
        // Add your 5000 questions here, e.g.:
        // allQuestions.add(new Question("What is 2+2?", new String[]{"3", "4", "5"}, 1));
        // ... repeat for all questions
    }
}
2. Shuffle the Question Bank Only on First App Launch

To ensure we only shuffle once (and keep the order consistent on subsequent launches), we can use SharedPreferences to track whether the initial shuffle has already happened. Here's how to integrate this into your app's entry point (like MainActivity or a custom Application class):

import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {
    private static final String PREFS_NAME = "QuizGamePrefs";
    private static final String KEY_IS_FIRST_LAUNCH = "is_first_launch";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        boolean isFirstLaunch = prefs.getBoolean(KEY_IS_FIRST_LAUNCH, true);

        QuizBank quizBank = new QuizBank(); // Initialize your question bank

        if (isFirstLaunch) {
            // Shuffle the full question list only once
            Collections.shuffle(quizBank.allQuestions);
            // Mark first launch as complete
            prefs.edit().putBoolean(KEY_IS_FIRST_LAUNCH, false).apply();
        }

        // Set up the available pool for the first game session
        quizBank.initializeAvailablePool();
    }
}

Key Notes:

  • Adjust the Question class fields to match your quiz's needs (e.g., add difficulty levels, category tags, etc.).
  • When resetting the pool (e.g., when a user starts a new game after finishing all questions), you can choose to reshuffle the available pool or keep the existing order—tweak the resetQuestionPool() method based on your preference.
  • For 5000 questions, using ArrayList is efficient enough; you won't notice any performance hits with these operations.

内容的提问来源于stack exchange,提问作者Giorgos Chatziioannidis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:33:05