我一直在编写一个将学生数据(姓名和年龄)存储在 .txt 文件中的程序。我现在正在做删除方法。但是,当用户输入一个字符串时,我希望它将输入与 my 中的字符串进行比较List<string>
,其中充满了名称。代码:
string tempFileName;
string inputSel; // Selection string for delete
Console.WriteLine(" -- Deleting Grade {0} -- ", grade);
Console.WriteLine("- Enter a student name to delete: ");
foreach (string file in fileNames)
{
tempFileName = file.Replace(".txt", "");
studentNames.Add(tempFileName);
}
foreach (string name in studentNames)
{
Console.Write("{0}\n", name);
}
Console.WriteLine();
Console.Write("> ");
inputSel = Console.ReadLine();
string input = inputSel.ToLower();
string tempString;
bool foundString = false;
foreach (string file in studentNames)
{
tempString = file.ToLower();
if (inputSel == tempString)
{
foundString = true;
}
}
if (!foundString)
{
Console.WriteLine("Wrong name entered!");
Console.WriteLine("Returning to grades menu..");
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
return;
}
如您所见,程序存储inputSel
到input
(ToLower()) 中,然后比较 studentNamesList<string>
中的每个字符串,如果找到匹配项,它会翻转 foundString bool
,但即使我输入了匹配的名称(例如,它说 JacobMusterson,我输入 JacobMusterson,它应该跳过“找不到学生”,但它没有。