简体   繁体   English

从文本文件android读取时随机化问题数组

[英]Randomize array of questions while reading from text file android

This is a android quiz app code snippet which load the question from text file. 这是一个android测验应用程序代码片段,可从文本文件加载问题。 I want to shuffle the question and answer after every next click so how can i implement random function ? 我想在每次单击后都将问题和答案打乱,那么如何实现随机函数? https://github.com/gitssk/quizfun/blob/master/src/ssk/quizfun/QuizFunActivity.java https://github.com/gitssk/quizfun/blob/master/src/ssk/quizfun/QuizFunActivity.java

https://github.com/gitssk/quizfun/blob/master/res/raw/questions.txt https://github.com/gitssk/quizfun/blob/master/res/raw/questions.txt

 private void loadQuestions() throws Exception {
        try {
        InputStream questions = this.getBaseContext().getResources()
                .openRawResource(R.raw.questions);
        bReader = new BufferedReader(new InputStreamReader(questions));
        StringBuilder quesString = new StringBuilder();
        String aJsonLine = null;
        while ((aJsonLine = bReader.readLine()) != null) {
            quesString.append(aJsonLine);
        }
        Log.d(this.getClass().toString(), quesString.toString());
        JSONObject quesObj = new JSONObject(quesString.toString());
        quesList = quesObj.getJSONArray("Questions");
        Log.d(this.getClass().getName(),
                "Num Questions " + quesList.length());
        } catch (Exception e){

        } finally {
            try {
                bReader.close();
            } catch (Exception e) {
                Log.e("", e.getMessage().toString(), e.getCause());
            }

        }


    }







https://github.com/gitssk/quizfun/blob/master/src/ssk/quizfun/QuizFunActivity.java

I will refrain from posting much code because I think you should attempt it on your own. 我将避免发布太多代码,因为我认为您应该自己尝试。 It is seriously not that tough. 真的不是那么难。 I will give you an approach though. 我会给你一个方法。

You have quesList = quesObj.getJSONArray("Questions"); 您有quesList = quesObj.getJSONArray("Questions"); . So quesList is the list of questions that is a JSONArray . 所以quesList是一个问题列表,它是一个JSONArray You want to shuffle this. 您想洗牌。 Just do this: 只要这样做:

  1. Get the length of the quesList array. 获取quesList数组的长度。 Let's call it len . 我们称它为len
  2. Create a simple arrayList called quesOrder containing integers 0 to len . 创建一个名为quesOrder的简单arrayList, quesOrder包含从0 to len整数。

     List<Integer> quesOrder = new ArrayList<>(); for (int i = 0; i <= len; i++) { quesOrder.add(i); } 

Once you have the quesOrder array. 一旦有了quesOrder数组。 Just do Collections.shuffle(quesOrder); 只需执行Collections.shuffle(quesOrder); . Now when you get questions from your quesList array, just get the index from quesOrder list. 现在,当您从quesList数组中获取问题时,只需从quesOrder列表中获取索引quesOrder And you will have a randomized selection. 您将有一个随机选择。 Put it together in a function for convenience. 为了方便起见,将其放在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM