所以我试图测试我的验证当用户键入错误的5位数字然后它会显示一个错误,并返回到原来的形式。但是,我的代码会使代码进入无限循环,因此我无法在表单上执行任何其他操作,因为循环不会结束。当没有匹配时,你如何摆脱无限循环?
public bool findCustomer(string accountNumber)
{
string record = Global.currentFile.getNextRecord(); //gets first record
bool okay = Global.customer.matchCustomer(accountNumber, record); //checks if it matches
while (!okay == true) //if it does not match, get next record and check again until it reaches end of file
{
record = Global.currentFile.getNextRecord();
okay = Global.customer.matchCustomer(accountNumber, record);
}
return okay;
}//end method
这里是从另一个类
public string getNextRecord()
{
string nextRecord = String.Empty;
while ((nextRecord = reader.ReadLine()) != null)
{
return nextRecord;
}
return nextRecord;
}// end getNextRecord
这里获取记录方法是在文本文件
12345 * Shrek * 1209 * 100000 * 50000
12077 * Sammy Wheeler * 1207 * 5000 * 0
99999 * The Big Grump * 1298 * 1500000 * 1500000
13579 * Brooks Robinson * 5555 * 225000 * 225000
24680 * Johnny Unitas * 1919 * 60000 * 34000
68420 * Y. A. Tittle * 1414 * 42000 * 12000
23456 * Hilary Clinton * 2222 * 65000 * 123456
23232 * Julianne Baird * 1234 * 145000 * 12321
我强烈建议您使用数据库系统。如果您需要将记录存储在文件中,请转至SQLite。你为什么试图重新发明轮子? –