提问者:小点点

我想为一个测验游戏随机化我的csv文件[重复]


我有我的测验游戏阅读一个csv文件来获取问题,但我不知道如何在每次开始游戏时随机化它们,我不知道我拥有的代码是否可能,我不想更改为sql或sqllite,因为我已经这样做了,我想这样完成,这是我读取文件的代码

import android.content.Context;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class CsvFileReader {

    public ArrayList<Question> readFile(Context ctx){
        ArrayList<Question> questions = new ArrayList<>();
        InputStream inputStream = ctx.getResources().openRawResource(R.raw.question);
        CSVFile csvFile = new CSVFile(inputStream);
        List<String[]> scoreList = csvFile.read();

      for (int i=1;i<scoreList.size();i++)
        {
            String[] strings = scoreList.get(i);

            int questionId = 0;
            String question = "";
            int dificulty = 0;
            int correctAnswer = 0;
            String answer1 = "";
            String answer2 = "";
            String answer3 = "";
            String answer4 = "";

            int length = strings.length;
            if (length>0){

                try {
                    questionId = Integer.parseInt(strings[0]);
                }catch (Exception ex){

                }

            }
            if (length>1){

                question = strings[1];
            }

            if (length>2){
                try {
                    dificulty = Integer.parseInt(strings[2]);
                }catch (Exception ex){

                }

            }

            if (length>3){
            try {
                correctAnswer = Integer.parseInt(strings[3]);
            }catch (Exception ex){

            }
            }


            if (length>4){
                answer1 = strings[4];

            }

            if (length>5){
                answer2 = strings[5];
            }

            if (length>6){
                answer3 = strings[6];
            }

            if (length>7){
                answer4 = strings[7];
            }



            Question questionData = new Question(questionId,question,dificulty,correctAnswer,answer1,answer2,answer3,answer4);


            questions.add(questionData);
        }
      
        return  questions;
    }
}

我希望每次我开始测验时都需要随机化问题,下面是文件中的代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CSVFile {
    InputStream inputStream;

    public CSVFile(InputStream inputStream){
        this.inputStream = inputStream;
    }

    public List read(){
        List resultList = new ArrayList();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            String csvLine;
            while ((csvLine = reader.readLine()) != null) {
                String[] row = csvLine.split(",");
                resultList.add(row);
            }
        }
        catch (IOException ex) {
            throw new RuntimeException("Error reading: "+ex);
        }
        finally {
            try {
                inputStream.close();
            }
            catch (IOException e) {
                throw new RuntimeException("Error while closing input stream: "+e);
            }
        }
        return resultList;
    }
}

如果有人需要更多代码才能进入或任何让我知道的东西,我将不胜感激。


共1个答案

匿名用户

您可以使用Collection. shuffle随机化一个arrayList。这个函数在java.util中。

像这样的东西:

import java.util.Collections;
...
public ArrayList<Question> readFile(Context ctx){
     ArrayList<Question> questions = new ArrayList<>();
     ...
     Collections.shuffle(questions);
     return questions;
}

这将返回一个随机的ArrayList