基于Processing实现读取TXT文件的简易测验应用及二维数组(2D array)数据处理问题
Got it, let's fix this up so you can access your quiz data using a 2D array like quizData[0][0] for the first question text and quizData[0][1] for its correct answer index. Here's a step-by-step solution:
Step 1: Declare a 2D Array to Store All Quiz Data
First, replace your existing global variables with a 2D String array. This will hold every question's details (question text, correct answer, and all four choices) as individual rows:
String[][] quizData; // 2D array to store all quiz questions and their data int currentQuestionIndex; // Tracks which question we're displaying
Step 2: Populate the 2D Array in setup()
In the setup() function, after loading your text file, we'll loop through each line, split it into parts, and assign each part to the corresponding position in the 2D array. Each row will have 6 elements:
- Index 0: Question text
- Index 1: Correct answer number
- Indexes 2-5: The four choices
Here's the updated setup():
void setup() { size(500, 500); background(255); currentQuestionIndex = 0; // Load all lines from the text file String[] lines = loadStrings("questions.txt"); // Initialize the 2D array: number of rows = number of questions, 6 columns per row quizData = new String[lines.length][6]; // Loop through each line to populate the 2D array for (int i = 0; i < lines.length; i++) { String[] questionParts = split(lines[i], ','); // Assign each part to the 2D array quizData[i][0] = questionParts[0]; // Question text (e.g., "questionA?") quizData[i][1] = questionParts[1]; // Correct answer index (e.g., "2") quizData[i][2] = questionParts[2]; // Choice 1 quizData[i][3] = questionParts[3]; // Choice 2 quizData[i][4] = questionParts[4]; // Choice 3 quizData[i][5] = questionParts[5]; // Choice 4 } }
Step 3: Access Data from the 2D Array in draw()
Now you can easily access any question's data using the 2D array indices. Let's update the draw() function to demonstrate this:
void draw() { // Stop looping once we've gone through all questions if (currentQuestionIndex >= quizData.length) { noLoop(); return; } // Access data from the 2D array for the current question String questionText = quizData[currentQuestionIndex][0]; String correctAnswer = quizData[currentQuestionIndex][1]; String choice1 = quizData[currentQuestionIndex][2]; String choice2 = quizData[currentQuestionIndex][3]; String choice3 = quizData[currentQuestionIndex][4]; String choice4 = quizData[currentQuestionIndex][5]; // Print to verify (you can replace this with your quiz UI code) println("Current Question: " + questionText); println("Correct Answer Index: " + correctAnswer); println("Choices: " + choice1 + ", " + choice2 + ", " + choice3 + ", " + choice4); println("---"); currentQuestionIndex++; }
Key Notes:
- To access the first question's text:
quizData[0][0](outputs "questionA?") - To access the first question's correct answer:
quizData[0][1](outputs "2") - The 2D array lets you jump to any question directly (e.g.,
quizData[2][0]gets the third question's text) instead of only iterating sequentially.
内容的提问来源于stack exchange,提问作者y2UcDgAL

