【问题标题】:I wanto randomize my csv file for a quizz game [duplicate]我想为问答游戏随机化我的 csv 文件 [重复]
【发布时间】:2021-06-22 10:56:50
【问题描述】:

我让我的问答游戏读取 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;
    }
}

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

【问题讨论】:

  • 顺便说一句,在read 中,您应该将resultList 声明为List&lt;String[]&gt;。一般you shouldn't use raw types.
  • 绝对有可能,有点棘手。您可以尝试通过在每次开始游戏时将它们复制到另一列随机化行来随机化问题。或者你可以在游戏过程中随机排列,但你也应该找到一种方法将问题布尔化为“已经问过”。就我个人而言,我会选择 OOP 和一个 JSON 文件来回答问题,它可以为您节省大量的字符串处理

标签: java android-studio kotlin


【解决方案1】:

您可以使用 Collections.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

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 2017-06-02
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多