【问题标题】:Looping in a method that returns boolean在返回布尔值的方法中循环
【发布时间】:2023-03-19 10:38:01
【问题描述】:

所以我在这里有一个用 C 语言编写的方法。我只需要了解它是如何工作的。

bool vote(string name)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes++;
            return true;
        }
            return false;
    }
}

*是的,这是来自 CS50,但这无关紧要。

所以我在这里有方法,我试图返回真或假。如果我将 true 放入循环中,它不会重复返回 true 直到该循环完成吗?怎样才能返回true一次?

【问题讨论】:

  • 如果你用 C 语言编写,为什么会有 java 标签?
  • @Moana Lisa 展示候选人的申报方式。否则你的问题将被否决。

标签: c loops boolean cs50


【解决方案1】:

一旦函数returns,循环被“放弃”并且不会/不能继续。

这个 for 循环只会执行 i = 0,因为如果 name 匹配 candidate[i].namereturn true 否则返回 false。

【讨论】:

    【解决方案2】:

    当您在函数中执行return 语句时,该函数就完成了。完成的!它只能返回一次。在您的代码中,循环将总是for 循环的第一次运行期间返回,因为即使if 测试失败,return false在里面 那个循环。

    您需要将return false 放在循环之外(之后):

    bool vote(string name)
    {
        for (int i = 0; i < candidate_count; i++)
        {
            if (strcmp(candidates[i].name, name) == 0)
            {
                candidates[i].votes++;
                return true; // Match found ... job done, function is finished!
            }
        //      return false; // Here, we would return on first loop that didn't match
        }
        return false; // Here, we return false if the loop finished without finding a match
    }
    

    【讨论】:

      【解决方案3】:

      strcmp 是一个比较两个字符串的函数。如果两个字符串相同,则返回零。看起来容器数组是一个对象数组。 任何对象的属性名称是否与所需名称相同,然后将该对象的投票计数属性增加 1,然后返回 true。返回 true 表示是的,列表中有一个具有该名称的候选人。如果列表中没有具有所需名称的候选者,则不会执行 if 块,并且控件将退出循环并执行 return false 语句,这表明没有所需名称的候选者。

      bool vote(string name)
      {
          for (int i = 0; i < candidate_count; i++)
          {
              //check if the name exists
              if (strcmp(candidates[i].name, name) == 0)
              {
                  //increment the vote of that candidate by 1.
                  candidates[i].votes++;
                  //return true stating there was a candidate in the list by that name.
                  return true;
              }
          }
          //return false stating that there was no candidate by that name.
          return false;
      }
      

      【讨论】:

        【解决方案4】:

        如果这是一个 C 代码,那么它是一个错误的函数定义,它依赖于全局变量,例如 candidate_countcandidates。您必须通过参数将这些变量传递给函数。

        如果假设候选者是一个元素类型类似于struct Candidate 的数组,那么函数应该至少声明为

        bool vote( const struct Candidate candidates[], size_t candidate_count, const char *name );
        

        此外,将 typedef 名称 string 用于类型 char * 也是一个坏主意,因为使用此 typedef 名称您不能声明类型 const char *

        你的函数定义的问题是返回语句

        return false;
        

        放在for循环内

        for (int i = 0; i < candidate_count; i++)
        {
            if (strcmp(candidates[i].name, name) == 0)
            {
                candidates[i].votes++;
                return true;
            }
                return false;
        } 
        

        while 应该在循环之后。

        for (int i = 0; i < candidate_count; i++)
        {
            if (strcmp(candidates[i].name, name) == 0)
            {
                candidates[i].votes++;
                return true;
            }
        } 
        
        return false;
        

        当这样一个简单的函数有多个返回语句时,这也不是一种好的编程风格。

        尽管如此,使用您的函数声明,如果按照以下方式重写它的定义,它的定义看起来会更清晰易读

        bool vote( string name )
        {
            int i = 0;
        
            while ( i < candidate_count && strcmp( candidates[i].name, name ) != 0 )
            {
                i++;
            }
        
            if ( i != candidate_count ) candidates[i].votes++;
        
            return i != candidate_count;
        }
        

        【讨论】:

          猜你喜欢
          • 2012-11-03
          • 1970-01-01
          • 1970-01-01
          • 2015-09-10
          • 2012-11-04
          • 2015-06-18
          • 2016-07-13
          • 1970-01-01
          相关资源
          最近更新 更多