簡體   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