【问题标题】:How to get the if statement out of the loop if it satisfies the condition?如果满足条件,如何将 if 语句移出循环?
【发布时间】:2021-05-23 20:23:00
【问题描述】:

我编写了一个代码,可以打印出用户输入字符串中有多少个 a。

我希望程序打印出“总共有 1 个 a”而不是“总共有 1 个 a”。

我尝试编写一个 if 语句,但它会在每个循环中打印出字符串的长度。我该如何调整这个?我已经尝试了一些东西,但我没有做到这一点。

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main(void){
    int count = 0;
    string input = get_string("Write something and I'll tell you how many a's there are: ");

    for (int i = 0, n = strlen(input); i < n ; i++){
        if (input[i] == 'a' || input[i] == 'A'){
             count++;
        }
        if (count == 1){
            printf("\nThere is a total of %i a", count);
        }
    }

    printf("\nThere are a total of %i a's", count);
}

【问题讨论】:

    标签: c if-statement printf cs50 conditional-operator


    【解决方案1】:

    只需将这一行从 for 循环中取出

    if (count == 1){
        printf("\nThere is a total of %i a", count);
    }
    

    然后在循环外构造这样的条件语句

    if (count == 1){
        printf("\nThere is a total of %i a", count);
    }else if(count > 1){
        printf("\nThere are a total of %i a's", count);
    }else{
        printf("\nThere are no a's");
    }
    

    这就是你的最终代码的样子

    int main(void){
    int ctr = 0;
    string input = get_string("Write something and I'll tell you how many a's there are: ");
    
    for (int i = 0; i < strlen(input) ; i++){
    
        if (input[i] == 'a' || input[i] == 'A'){
             count++;
        }
    
        //Where you removed the if (count == 1)
    
    }
    
    //Where it should be
    if (count == 1){
        printf("\nThere is a total of %i a", count);
    }else if(count > 1){
        printf("\nThere are a total of %i a's", count);
    }else{
        printf("\nThere are no a's");
    }
    

    }

    【讨论】:

      【解决方案2】:

      你可以使用 if 语句来编写。

      #include <stdio.h>
      #include <cs50.h>
      #include <string.h>
      
      int main(void){
          int count = 0;
          string input = get_string("Write something and I'll tell you how many a's there are: ");
      
          for ( size_t i = 0, n = strlen(input); i < n ; i++){
              if (input[i] == 'a' || input[i] == 'A'){
                   count++;
              }
          }
      
          if (count == 1){
              printf("\nThere is a total of %i a", count);
          }
          else {
              printf("\nThere are a total of %i a's", count);
          }
      }
      

      或者你可以使用条件运算符

      #include <stdio.h>
      #include <cs50.h>
      #include <string.h>
      
      int main(void){
          int count = 0;
          string input = get_string("Write something and I'll tell you how many a's there are: ");
      
          for ( size_t i = 0, n = strlen(input); i < n ; i++){
              if (input[i] == 'a' || input[i] == 'A'){
                   count++;
              }
          }
      
      
          printf("\nThere %s a total of %i a%s", 
                 count == 1 ? "is" : "are", 
                 count, 
                 count == 1 ? "" : "'s" );
      }
      

      第三种方法可以使用指定格式字符串的字符串文字。例如

      #include <stdio.h>
      #include <cs50.h>
      #include <string.h>
      
      int main(void){
          int count = 0;
          string input = get_string("Write something and I'll tell you how many a's there are: ");
      
          for ( size_t i = 0, n = strlen(input); i < n ; i++){
              if (input[i] == 'a' || input[i] == 'A'){
                   count++;
              }
          }
      
          string format;
      
          if (count == 1){
              format = "\nThere is a total of %i a";
          }
          else {
              format = "\nThere are a total of %i a's";
          }
      
          printf( format, count );
      }
      

      【讨论】:

      • 拜托弗拉德,你能给我解释一下string 类型是什么时候在C 中引入的。我相信你知道它来自最近的标准,但我不知道。我在哪里可以获得关于我不​​知道的这种类型的信息。提前致谢。 (它让我对 c++ string 类型感到困惑,但你不能将strlen() 应用于它,所以我觉得它是char * 的别名,是吗?)
      • @LuisColorado 类型字符串是类型 char 的 typedef 别名(定义在头文件
      • 感谢弗拉德,感谢您的解释。
      【解决方案3】:
      #include <stdio.h>
      #include <cs50.h>
      #include <string.h>
      
      int main(void){
          int count = 0;
          string input = get_string("Write something and I'll tell you how many a's there are: ");
      
      for (int i = 0, n = strlen(input); i < n ; i++)
              if ((input[i] | 0x20) == 'a')
                  count++;
      
          (count == 1) ? printf("\nThere is a total of 1 a") : printf("\nThere are a total of %i a's", count);
      
      }
      

      使用ternary operator

      注意:('a' or 0x20) 以及 ('A' or 0x20) 等于 'a'

      【讨论】:

      • 我还不知道三元是什么,因为我在课程中没有遇到过,但我一定会研究一下!它成功了。谢谢!
      • 不客气,我添加了解释它的维基百科链接!
      • NB: ('a' or 0x20) as well as ('A' or 0x20) equal 'a' - 仅当系统使用 ASCII 时。不便携和幼稚。 @ano - 避免这种微伪优化作为斑块。它们看起来很“hacky”,但实际上并非如此。
      【解决方案4】:

      如果您想在满足条件后退出循环,您可以使用break 语句,它将使您退出内部循环。

      【讨论】:

        【解决方案5】:
        int main(void)
        {
            int count = 0;
            string input = get_string("Write something and I'll tell you how many a's there are: ");
            
        
            for (size_t i = 0, n = strlen(input); i < n ; i++)
                count += (input[i] == 'a' || input[i] == 'A');
            
            printf("\nThere is a total of %i a%s", count, count == 1 ? "" :"'s");
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-18
          • 2016-02-27
          • 1970-01-01
          • 2016-08-06
          相关资源
          最近更新 更多