使问题以随机顺序显示

时间:2017-10-01 17:34:50

标签: java android

正如你在标题中看到的那样,我想让我的问题以随机顺序出现,但我不知道如何。我有4个按钮监听器,在您回答问题后更新问题,我的更新问题如下所示:

private void updateQuestion(){
    mQuestionView.setText(mQuestionLibrary.getQuestionFrankrijk(mQuestionNumber));
    mButtonChoice1.setText(mQuestionLibrary.getChoice1Frankrijk(mQuestionNumber));
    mButtonChoice2.setText(mQuestionLibrary.getChoice2Frankrijk(mQuestionNumber));
    mButtonChoice3.setText(mQuestionLibrary.getChoice3Frankrijk(mQuestionNumber));
    mButtonChoice4.setText(mQuestionLibrary.getChoice4Frankrijk(mQuestionNumber));

    mAnswerFrankrijk = 
mQuestionLibrary.getCorrectAnswerFrankrijk(mQuestionNumber);
    mQuestionNumber++;
}

我还有一个问题库,我想我必须提出不同的问题,这样我才能让它们以不同的顺序显示,但我不知道在我的代码中应用的方式和位置

Questionlibrary:

public class QuestionLibrary {

    public static String mQuestionsFrankrijk [] = {
            "Wat is de hoofdstad van Frankrijk?",
            "Wat is de bijnaam van het Franse nationale voetbalelftal",
            "Welke van de volgende landen grenst niet aan Frankrijk?",
            "Bij welke sport hoort 'le maillot jaune'?",
            "Welk museum in Parijs heeft een piramide als ingang?",
            "Hoeveel inwoners heeft Frankrijk?",
            "Het Franse volkslied is een?",
            "Het vliegveld bij Parijs heet",
            "De Romeinse naam voor Parijs is?",
            "Hoeveel departemeneten zijn er in Frankrijk?"
    };

    public static String mChoicesFrankrijk [][] = {
            {"Lyon", "Parijs", "Nice", "Bordeaux"},
            {"La France", "Le Coq Sportif", "Les Bleus", "Les Gagnants"},
            {"Zwitserland", "België", "Spanje", "Oostenrijk"},
            {"Tennis", "Wielrennen", "Rugby", "Cricket"},
            {"Musée d'Orsay", "Musée Rodin", "Louvre", "Centre Georges Pompidou"},
            {"50 miljoen", "60 miljoen", "70 miljoen", "80 miljoen"},
            {"Ballade", "Chanson", "Hymne", "Ode"},
            {"Horla", "Lorly", "Orah", "Orly"},
            {"Arelate", "Augustodonum", "Lugdunum", "Lutetia"},
            {"66", "76", "86", "96"}

    };

    public String mCorrectAnswersFrankrijk [] =
    {"Parijs", "Les Bleus", "Oostenrijk", "Wielrennen", "Louvre", "60 miljoen", "Hymne",
     "Orly", "Lutetia", "96"};



    public String getQuestionFrankrijk(int a) {
        return mQuestionsFrankrijk[a];
    }
    public String getChoice1Frankrijk(int a) {
        return mChoicesFrankrijk[a][0];
    }
    public String getChoice2Frankrijk(int a) {
        return mChoicesFrankrijk[a][1];
    }
    public String getChoice3Frankrijk(int a) {
        return mChoicesFrankrijk[a][2];
    }
    public String getChoice4Frankrijk(int a) {
        return mChoicesFrankrijk[a][3];
    }

    public String getCorrectAnswerFrankrijk(int a) {
        return mCorrectAnswersFrankrijk[a];
    }

1 个答案:

答案 0 :(得分:0)

您可以创建一个新类Question,它封装了与问题相关的所有信息,而不是将您的问题,选择和答案组织到3个单独的数组中:

public class Question {
    private String mQuestionText;
    private List<String> mChoices;
    private String mAnswer;

    public Question(String questionText, @Size (4) String[] choices, String answer)
        mQuestionText = questionText;
        mChoices = Arrays.asList(choices);
        mAnswer = answer;
    }

    public String getQuestionText() {
        return mQuestionText;
    }

    public List<String> getChoices() {
        return mChoices;
    }

    public String getAnswer() {
        return mAnswer;
    }
}

现在,您可以将这些Question个对象整理到List中的QuestionLibrary。通过这样做,您将能够使用java.util.Collections.shuffle()实用程序方法随机重新排序列表。您可能希望在QuestionLibrary类中包含一个构造函数,为您完成所有这些初始设置:

e.g。

public class QuestionLibrary {
    List<Question> mQuestionsList; // your List of Questions

    public QuestionLibrary() {
        mQuestionsList = new ArrayList<Question>();

        // add each question and corresponding choices / answers into the list
        mQuestionsList.add(new Question(
            "Wat is de hoofdstad van Frankrijk?",
            {"Lyon", "Parijs", "Nice", "Bordeaux"},
            "Parijs"
        ));

        ...

        mQuestionsList.add(new Question(
            "Hoeveel departemeneten zijn er in Frankrijk?",
            {"66", "76", "86", "96"},
            "96"
        ));

        // randomize the order of the list
        Collections.shuffle(mQuestionList);
    }
}

在实例化QuestionLibrary对象后,列表的顺序将被随机化。然后,您可以在QuestionLibrary课程中创建一个getter方法,以便只检索列表中的下一个Question

public class QuestionLibrary {

    ...

    public Question getNextQuestion() {
        if (mQuestionList.isEmpty()) {
            return null; // no more Questions left
        } else {
           return mQuestionList.remove(0); // remove and return the next Question from the list
        }
    }
}

最后,您可以修改updateQuestion方法以获取下一个可用Question(如果有),并相应地更新您的用户界面:

private void updateQuestion() {
    Question nextQuestion = mQuestionLibrary.getNextQuestion();
    if (nextQuestion == null) {
        // No more questions! Do something here
    } else {
        // Get the question text, answer and choices
        String questionText = nextQuestion.getQuestionText();
        String answer = nextQuestion.getAnswer();
        List<String> choices = nextQuestion.getChoices();
        Collections.shuffle(choices); // you can shuffle the order of the choices, too

        // update your UI using the above
        ...

    }
}
相关问题