[英]Getting streamreader to read specific line in text file, then write to console c#
我有一个包含4个变量类型的结构,该结构的数据保存在一个文本文件中。 结构由问题编号,问题级别,问题和问题答案组成。 我正在尝试仅将问题打印到控制台窗口,但是atm程序坚持首先编写问题编号,然后是级别,最后是问题,然后是问题答案。 我只想将问题打印到控制台屏幕。 这是我到目前为止的内容:
static void quiz(QuestionStruct[] _quiz)
{
bool asked = true;
int score = 0;
int AmountAsked = 0;
string level = "1";
string ans;
int pos = 0;
var pathToFile = @"..\..\..\Files\questions.txt";
using (StreamReader sr = new StreamReader(pathToFile, true))
{
while (AmountAsked < 20 || score >= 50)
{
Console.Clear();
//int pos = 0;
if (level == "1")
{
AmountAsked++;
questions[pos].Question = sr.ReadLine();
Console.Write(questions[pos].Question);
ans = Console.ReadLine();
if (ans == questions[pos].answer)
{
level = "2";
score = score + 1;
while (questions[pos].Level != "2")
{
pos++;
}
}
}
}
}
看来您没有以正确的顺序读取文件。 您将文件的第一行存储在Question成员中,并将第二行存储在Answer成员中。 然后,您再也不会读到有关问题级别的行。 这与文件布局不匹配。
另外,由于代码执行过多,因此很难看到代码中的问题。 您应该重构代码,以便读取文件是一种方法,然后执行测验是另一种方法。 这是称为关注点分离的软件设计原理,网址为http://en.wikipedia.org/wiki/Separation_of_concerns 。
这是重构的代码。 如果您跳过了阅读行,或者使用此代码乱读了行,那将更容易发现。
public void Quiz()
{
List<QuestionStruct> questions = GetQuestions(@"..\..\..\Files\questions.txt");
RunQuiz(questions);
}
// My job is to parse the file
public List<QuestionStruct> GetQuestions(string pathToFile)
{
List<QuestionStruct> questions = new List<QuestionStruct>();
using (StreamReader sr = new StreamReader(pathToFile, true))
{
while (!sr.EndOfStream)
{
QuestionStruct q = new QuestionStruct();
q.Number = sr.ReadLine();
q.Level = sr.ReadLine();
q.Question = sr.ReadLine();
q.Answer = sr.ReadLine();
questions.Add(q);
}
}
return questions;
}
// My job is to run the quiz
public void RunQuiz(List<QuestionStruct> questions)
{
bool asked = true;
int score = 0;
int AmountAsked = 0;
string level = "1";
string ans;
int pos = 0;
foreach (QuestionStruct question in questions)
{
if (!(AmountAsked < 20 || score >= 50))
break;
Console.Clear();
//int pos = 0;
if (level == "1")
{
AmountAsked++;
Console.Write(question.Question);
ans = Console.ReadLine();
if (ans == question.Answer)
{
level = "2";
score = score + 1;
while (question.Level != "2")
{
pos++;
}
}
}
}
}
如果问题编号,级别,问题和答案有单独的行,则必须按顺序执行4 sr.ReadLine()
,以将每条正确的行分配给strcut的成员。
static void quiz(QuestionStruct[] _quiz)
{
bool asked = true;
int score = 0;
int AmountAsked = 0;
string level = "1";
string ans;
int pos = 0;
var pathToFile = @"..\..\..\Files\questions.txt";
using (StreamReader sr = new StreamReader(pathToFile, true))
{
while (AmountAsked < 20 || score >= 50)
{
Console.Clear();
//int pos = 0;
while(!sr.EndOfStream)
{
if (level == "1")
{
AmountAsked++;
// Remember the order in which you are storing the values to each member of struch
questions[pos].number = sr.ReadLine();
questions[pos].level = sr.ReadLine();
questions[pos].Question = sr.ReadLine();
questions[pos].answer = sr.ReadLine();
Console.Write(questions[pos].Question);
ans = Console.ReadLine();
if (ans == questions[pos].answer)
{
level = "2";
score = score + 1;
while (questions[pos].Level != "2")
{
pos++;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.