【发布时间】:2014-04-15 20:57:30
【问题描述】:
我正在尝试按他们居住的城镇搜索存储在文本文件中的客户帐户。三个客户帐户都属于不同的城镇。我认为该程序找到了正确的客户,因为它确实返回要写入屏幕的数据,但不是全部。这些图像更好地解释了它。试图嵌入它们但没有积分
文本文件内容:
搜索居住在利物浦的客户时(虽然图片没有显示,但光标开始在 DOB 下方闪烁 5 行)
正在寻找贝尔法斯特客户。请注意,它获取了 coreect DOB 但错误的客户编号和姓氏。(我会把链接放在评论中它不会让我发布超过 2 个链接)
这是方法的代码::
static void FindTown(CustomerStruct[] CustomerDeats)
{
string City;
bool CityMatch = false;
Console.Clear();
begin:
try
{
Console.WriteLine("Please enter the customers Town ");
City = Console.ReadLine();
}
catch
{
Console.WriteLine("Failed. Please try again.");
goto begin;
}
var pathToTown = @"..\..\..\Files\Customer.txt";
using (StreamReader sr = new StreamReader(pathToTown))
while (!CityMatch)
{
{
RecCount = 0;
CustomerDeats[RecCount].Town = sr.ReadLine();
if (City == CustomerDeats[RecCount].Town)
{
Console.WriteLine("\n\n");
CityMatch = true;
CustomerDeats[RecCount].CustomerNo = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].CustomerNo);
CustomerDeats[RecCount].Surname = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].Surname);
CustomerDeats[RecCount].Forename = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].Forename);
CustomerDeats[RecCount].Street = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].Street);
CustomerDeats[RecCount].Town = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].Town);
CustomerDeats[RecCount].DOB = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].DOB);
Console.ReadKey();
}
RecCount++;
}
}
}
【问题讨论】:
-
在任何现代编程语言中使用
goto很少是一个好主意。它可能导致难以理解和维护的非结构化代码。 -
我会记住这一点并确保我熟悉它。
-
这就是发生的事情...... Eep! xkcd.com/292
标签: c# streamreader