【问题标题】:How do you get out of infinite loop when there's no match?没有匹配项时如何摆脱无限循环?
【发布时间】:2016-06-21 07:21:05
【问题描述】:

所以我试图测试我的验证,当用户输入错误的 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。你为什么要重新发明轮子?

标签: c# .net file while-loop


【解决方案1】:

你应该处理getNextRecord()返回null或空字符串的情况

while (!okay)                                                       
{
     record = Global.currentFile.getNextRecord();   
     if (string.IsNullOrWhiteSpace(record)
         break;   
     okay = Global.customer.matchCustomer(accountNumber, record);
}

请注意,如果文件在其数据中间包含一个空字符串,那么这将失败并且该空行之后的行将被转义。

为什么要同时检查nullstring.Empty?因为如果阅读器到达文件末尾,则nextRecord 为空。

【讨论】:

    【解决方案2】:

    在您的代码中,没有下一条记录意味着您将返回string.Empty

    public string getNextRecord()
    {
        string nextRecord = String.Empty;
    
        while ((nextRecord = reader.ReadLine()) != null)
        {
            return nextRecord;
        }
    
            return nextRecord;
    }// end getNextRecord
    

    您可以简单地使用此信息来退出您的循环:

    record = "initval";
    while (!okay == true && !string.IsNullOrEmpty(record))                                                       //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);
    }
    

    尽管如此,您仍可以通过删除 getNextRecord() 并将 !okay == true 更改为 !okay 来进一步简化代码:

    public bool findCustomer(string accountNumber)
    {
        string record = reader.ReadLine(); //gets first record
        bool okay = Global.customer.matchCustomer(accountNumber, record);   //checks if it matches
        while (!okay && !string.IsNullOrEmpty(record))                                                       //if it does not match, get next record and check again until it reaches end of file
        {
             record = reader.ReadLine(); //why not this?
             if (record != null)  
                 okay = Global.customer.matchCustomer(accountNumber, record);
        }
    
            return okay;                                                                                  
    }//end method
    

    【讨论】:

    • 你应该检查record的空值和空值。
    • !okay == true 完全错误。!okay 会解决您的问题。好吧,也许不是全部,但通过几张海报指出这一点似乎非常重要。
    • @hylander0 实际上,它更多的是冗余而不是重要。 :) 但对于某些人来说,摆脱冗余很重要。因此它重要的。
    • @hylander0 出于同样的原因,我去掉了函数getNextRecord,因为它是... 冗余 ;)
    【解决方案3】:

    首先,一个小建议(!okay == true)(!okay) 相同,只是个人喜好。关于代码,IMO,您永远不会检查您是否已完成阅读文件。当没有更多记录时,您只需返回 "" 并且您的代码会继续调用 Global.customer.matchCustomer(accountNumber, "");,它总是返回 false,因此是无限循环。考虑while (!okay && record != String.Empty)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 2021-03-17
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多