[英]Using a for loop with a stream writer
我正在嘗試創建具有多個屬性的問題對象,所有這些屬性都存儲在文本文件中,並使用StreamReader檢索。
一個屬性是數組Choices[]
。 我需要所說的數組能夠基於qType
變量的值進行填充。
例如: if qType = "button"
,那么Choices[]
應該只從文件讀取4行。 而if qType = "dragdrop"
則Choices[]
應該從文件中讀取6次。 到目前為止,我已經嘗試過使用for循環,case語句和if語句-所有這些都已中斷。 有人可以告訴我如何在不中斷StreamReader的情況下做到這一點嗎?
這是有效的代碼:
using (var quizFileReader = new System.IO.StreamReader("PhysQuestions.txt"))
{
string line;
Question question;
// Loop through the lines of the file until there are no more (the ReadLine function return null at this point).
// ReadLine called here only reads question texts (first line of a question), while other calls to ReadLine read the choices.
while ((line = quizFileReader.ReadLine()) != null)
{
// Skip this loop if the line is empty.
if (line.Length == 0)
continue;
// Create a new question object.
// The "object initializer" construct is used here by including { } after the constructor to set variables.
question = new Question()
{
// Set the question text to the line just read.
QuestionText = line,
qType = quizFileReader.ReadLine(),
// Set the choices to an array containing the next 4 lines read from the file.
Choices = new string[]
{
quizFileReader.ReadLine(),
quizFileReader.ReadLine(),
quizFileReader.ReadLine(),
quizFileReader.ReadLine(),
},
hintTxt = quizFileReader.ReadLine(),
difficulty = Convert.ToDouble(quizFileReader.ReadLine()),
imgPath = quizFileReader.ReadLine()
};
}
}
這是我嘗試做的不起作用的事情:
Choices = new string[]
{
for(int i=0; i<4; i++)
{
quizFileReader.ReadLine(), // expects a ;
}
}, // excepts a ;
這是我正在使用的完整代碼:
public string QuestionText, imgPath, hintTxt; // Actual question text.
public string[] Choices; // Array of answers from which user can choose.
public int Answer, qNum, linesToRead; // Index of correct answer within Choices.
public double difficulty; // Double that represents difficulty of each question
public List<Question> getQues() // reads questions from text file, assigns all strings in text file to index of List, returns the full list of questions
{
// Create new list to store all questions.
var questions = new List<Question>();
// Open file containing quiz questions using StreamReader, which allows you to read text from files easily.
using (var quizFileReader = new System.IO.StreamReader("PhysQuestions.txt"))
{
string line;
Question question;
// Loop through the lines of the file until there are no more (the ReadLine function return null at this point).
// ReadLine called here only reads question texts (first line of a question), while other calls to ReadLine read the choices.
while ((line = quizFileReader.ReadLine()) != null)
{
// Skip this loop if the line is empty.
if (line.Length == 0)
continue;
// Create a new question object.
// The "object initializer" construct is used here by including { } after the constructor to set variables.
question = new Question()
{
// Set the question text to the line just read.
QuestionText = line,
linesToRead = Convert.ToInt32(quizFileReader.ReadLine()),
Choices = new string[linesToRead];
for (int i=0; i < linesToRead; i++)
{
Choices[i] = await quizFileReader.ReadLineAsync();
}
hintTxt = await quizFileReader.ReadLineAsync();
difficulty = Convert.ToDouble(quizFileReader.ReadLine());
imgPath = quizFileReader.ReadLine();
};
// Set correct answer to -1, this indicates that no correct answer has been found yet.
question.Answer = -1;
// Check each choice to see if it begins with the '!' char (marked as correct).
for (int i = 0; i < 4; i++)
{
if (question.Choices[i].StartsWith("!"))
{
// Current choice is marked as correct. Therefore remove the '!' from the start of the text and store the index of this choice as the correct answer.
question.Choices[i] = question.Choices[i].Substring(1);
question.Answer = i;
break; // Stop looking through the choices.
}
}
// Check if none of the choices was marked as correct. If this is the case, we throw an exception and then stop processing.
if (question.Answer == -1)
{
throw new InvalidOperationException(
"No correct answer was specified for the following question.\r\n\r\n" + question.QuestionText);
}
// Finally, add the question to the complete list of questions.
questions.Add(question);
}
return questions;
}
}
一旦知道要讀取多少行,便可以執行以下操作:
using (var quizFileReader = new System.IO.StreamReader("PhysQuestions.txt"))
{
Question question;
int linesToRead = 4;
string[] choices = new string[linesToRead];
for (int i=0; i < linesToRead; i++)
{
choices[i] = await quizFileReader.ReadLineAsync();
}
}
那應該使您朝着正確的方向開始,但是如果您陷入困境,請告訴我。
我還將ReadLine轉換為它的異步版本,因此它不會被阻塞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.