繁体   English   中英

我想制作一个带处理的二维数组

[英]I want to make a 2D array with processing

我正在尝试通过从 txt 文件中读取数据来制作一个简单的测验应用程序。 我不知道如何处理数据。

问题.txt

questionA?,2,choicesA-1,achoicesA-2,choicesA-3,choicesA-4
questionB?,3,choicesB-1,choicesB-2,choicesB-3,choicesB-4
questionC?,4,choicesC-1,choicesC-2,choicesC-3,choicesC-4

培训.pde

String lin;
int ln;
String lines[];

void setup() {
  size(500, 500);
  background(255);

  ln=0;
  lines = loadStrings("questions.txt");
}

void draw() {
  lin =lines[ln];
  String[] co = split(lin, ',');
   println(co[0]);
   println(co[1]);
  ln++;
  if (ln == lines.length) noLoop();
}

我想做一个二维数组..

   println(co[0][0]); //questionA?
   println(co[0][1]); //2

您可以使用加载和读取行。 将每一行存储在一个 Arraylist 中:

List<String> lines = Collections.emptyList(); 
try {
    lines =  Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8); 
} catch (IOException e) {
    // do something
    e.printStackTrace();
} 

现在,对于 Arraylist 中的每一行,您可以使用分隔符','将它们分开','并使用以下单词做任何您想做的事情:

 for (String line: lines) {
     String[] words = Arrays.stream(line.split(",")).map(String :: trim).toArray();
     System.out.println("Question: " + words[0] + ", Choices: " + words[1] + " " + words[2] + " " + words[3] + " " + words[4]);
 }

你可以使用这样的东西:

String filename = "path_to_file";
File file = new File(filename);
Scanner data = new Scanner(file);

String line;
List<String[]> questions = new ArrayList<>();
while (data.hasNextLine()) {
     line = data.nextLine();
     questions.add(line.split(","));
}

如您所见,问题是一个字符串数组列表。 该列表的每个元素对应于文件的一行,您可以使用 questions.get(index) 访问一行。 您还可以使用标准数组表示法获取行的特定元素

例如,使用您提供的示例, questions.get(1)[2]将返回choicesB-1

请记住,从纯文本 (.txt) 中读取数据总是会让您头疼。 你应该使用像 JSON 或 YAML 这样的格式来存储你的数据,一个好的解析器已经可用。

Alberto 和 devReddit 建议的字符串解析选项很好 (+1)。

我想建议将数据重组为JSON 数组

[
    {
      "question":"Question A ?",
      "options": [
        "choicesA-1",
        "choicesA-2",
        "choicesA-3",
        "choicesA-4"
        ],
      "selectedIndex":-1,
      "correctIndex":1
    },
    {
      "question":"Question B ?",
      "options": [
        "choicesB-1",
        "choicesB-2",
        "choicesB-3",
        "choicesB-4"
        ],
      "selectedIndex":-1,
      "correctIndex":2
    },
    {
      "question":"Question C ?",
      "options": [
        "choicesC-1",
        "choicesC-2",
        "choicesC-3",
        "choicesC-4"
        ],
      "selectedIndex":-1,
      "correctIndex":3
    }
  ]

如果你将它保存在一个新的草图文件夹中作为questionnaire.json .json,你可以加载它并解析(以及检查)答案如下:

// stores currently loaded questions
JSONArray questions;
// the index of the current question
int currentQuestionIndex = 0;
// the questionnaire data at this index (question, options, correct answer(as index), selected answer(as index)) 
JSONObject currentQuestionData;

void setup() {
  size(400, 400);
  // load data
  try {
    questions = loadJSONArray("questionnaire.json");
  }
  catch(Exception e) {
    println("error loading JSON data");
    e.printStackTrace();
    exit();
  }
}

void draw() {
  // get the current question data
  currentQuestionData = questions.getJSONObject(currentQuestionIndex);
  // access the choices associated with this question
  JSONArray currentQuestionOptions = currentQuestionData.getJSONArray("options");

  background(0);
  // render the question
  text(currentQuestionData.getString("question"), 10, 15);
  // ...and the choices
  for (int i = 0; i < currentQuestionOptions.size(); i++) {
    text(currentQuestionOptions.getString(i), 10, 35 + (20 * i));
  }
  // testing view:
  // render user selected index
  text("selected: " + currentQuestionData.getInt("selectedIndex"), 10, 120);
  // render correct index (and choice text)
  text("correct: " + currentQuestionData.getInt("correctIndex") + " = " + currentQuestionOptions.getString(currentQuestionData.getInt("correctIndex")), 10, 150);
  // render match condition (result) 
  text("match: " + (currentQuestionData.getInt("selectedIndex") == currentQuestionData.getInt("correctIndex")), 10, 180);
}

void keyPressed() {
  // control question with left/right keys
  if (keyCode == LEFT && currentQuestionIndex > 0) currentQuestionIndex--;
  if (keyCode == RIGHT && currentQuestionIndex < questions.size() - 1) currentQuestionIndex++;
  // set user answers on 1,2,3,4 keys
  if (key == '1') currentQuestionData.setInt("selectedIndex", 0);
  if (key == '2') currentQuestionData.setInt("selectedIndex", 1);
  if (key == '3') currentQuestionData.setInt("selectedIndex", 2);
  if (key == '4') currentQuestionData.setInt("selectedIndex", 3);
}

这是相当多的代码,因为除了展示如何解析和呈现问题/选择之外,它还演示了如何使用左/右键以及带有 1,2,3,4 的测试箭头来测试通过问题键。 希望评论说明了功能。

存储用户选择的选择索引的决定是可选的,更多的是一种想法。 稍后存储测验结果可能很有用( saveJSONArray()可以提供帮助)。

另一种选择可能是将数据存储为 CSV 表,Processing 可以使用loadTable()轻松解析该表。

我个人认为 JSON 格式更灵活,即使它更冗长:您可以使用树结构/具有不同选项数量的问题等,而 CSV 表仅限于一个表。

加载/保存字符串时的最后一句话:注意特殊字符。

玩得开心!

暂无
暂无

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

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