使用列表显示文本文件中的问题



我正在尝试为一个琐事游戏制作一个程序,其中用户一次从csv文本文件中被问到一个问题。每个问题都有一个分值 1-3。如果有答案匹配,他们赢得该问题的分数,如果他们错了,他们不会得到分数,这将向他们显示答案是什么。该程序将运行,直到用户回答所有问题,然后显示玩家总数。我将问题答案和要点存储在 3 个列表中,但 我不确定如何一次向用户显示 10 个问题 1 行。 任何帮助将不胜感激

这是我到目前为止得到的

// The path of the file to write to.
// string filename = "C:\Intel\Trivia.csv";
static void LoadData(
string filePath,
List<string> questionList,
List<string> answersList,
List<int> pointsList
)
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader reader = new StreamReader(filePath))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = reader.ReadLine()) != null)
{
string[] lineArray = line.Split(",");
string question = lineArray[0];
string answer =   lineArray[1];
int point = int.Parse(lineArray[2]);
questionList.Add(question);
answersList.Add(answer);
pointsList.Add(point);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read.");
Console.WriteLine(e.Message);
}
}
static void Main(string[] args)
{
const string filename = "C:\Intel\Trivia.csv";
// Declare and create three parallel list to store the data
List<string> questionList = new List<string>();
List<string> answerList = new List<string>();
List<int> pointList = new List<int>();
// Call the LoadData method to populate the parallel lists using data from the text file
LoadData(filename, questionList, answerList, pointList);


}
}

您的具体问题似乎是"如何一次向用户显示 10 个问题 1 行"?

答案是循环浏览问题并等待用户输入。

for (int i = 0; i < questionList.Count; i++)
{
Console.WriteLine(questionList[i]);
Console.Write("Answer? ");
string answer = Console.ReadLine();
// Now do comparison logic to see if the answer is correct, calculate points, etc
}

最新更新