【问题标题】:C: My loop doesn't seem to workC:我的循环似乎不起作用
【发布时间】:2013-12-03 15:32:30
【问题描述】:

我是 C 新手,今天遇到了一个我还没有弄清楚的问题,所以我需要一些帮助。我们被赋予了这个任务:

'编写一个程序来演示一个使用“预读”技术的“while”循环:它要求用户输入 10 和 100 之间(包括)10 和 100 之间的数字,输入零会终止循环。如果输入小于 10 或大于 100 的数字,则会显示错误消息(例如“错误:不允许 250”)。循环终止后,程序会打印输入的数字数量。'

我遇到的问题是,一旦我输入了一个有效数字(10-100 之间),程序就会静止不动,它既不会终止也不会循环。另一方面,如果我输入一个无效的数字,比如 8, 102,它会循环 printf("Error, %d is not allowed\n", num);

代码如下:

#include <stdio.h>


main(void)

{

int num;
int counter=1;

printf("Please type in your number between (and including) 10-100\n");
scanf("%d", &num);

if((num <10) || (num >100))
{
    printf("Error, %d is not allowed\n", num);
}

while (num > 0)
{
    counter++;

    if((num <10) || (num >100))
    {
        printf("Error, %d is not allowed\n", num);
    }
    counter++;
}


printf("%d numbers were entered!\n", counter);

}

【问题讨论】:

  • 您需要 scanf(...) 循环的每次迭代来获取一个新数字。

标签: c loops while-loop


【解决方案1】:

已经分享的答案是正确的,您需要在循环中请求输入。此外,您需要为用户提供退出循环的方法(例如哨兵),如果您知道输入将是非负的,那么您可以将变量定义为“unsigned int”而不是只是“int”。方法见下文。

    #include <stdio.h>

    int main(void)
    {
       unsigned int num = 0, counter = 0;

       while (num != -1) // need to control with a sentinel to be able to     exit
    {
        printf("Please type in your number between (and including) 10-100. Enter \"-1\" to exit.\n");
        scanf("%d", &num);
        counter++; // increase the count by one each successful loop

        // set the non-allowed numbers
        if((num <10) || (num >100))
        {
            printf("Error, %d is not allowed\n", num);
            counter--; // Need to subtract 1 from the counter if enter a non-allowed number, else it will count them
        }

    } // end while loop

       printf("%d numbers were entered!\n", counter); // Show the use how many correct numbers they entered


    } // end main

【讨论】:

    【解决方案2】:

    这是因为您必须从循环内的输入 (scanf) 中获取数字。否则循环永远循环(如果输入的最合适的数字> 0)。如果它无效,您会看到输出。

    【讨论】:

      【解决方案3】:
      #include <stdio.h>
      
      void main(void)
      {
          int num = 0, counter = 0;
      
          printf("Please type in your number between (and including) 10-100\n");
      
          do
          {
              printf("? ");
              scanf("%d", &num);
              if (!num)
                  break;
              else if((num < 10) || (num > 100))
                  printf("Error, %d is not allowed\n", num);
              counter++;
          } while (num);
      
          printf("%d numbers were entered!\n", counter);
      
      }
      

      【讨论】:

        【解决方案4】:

        您的 while 循环陷入了无限循环。您设置了“WHILE (num > 0)”的条件,但是您从未真正更改它。这就是为什么当您的 num 在界限内时代码会被捕获。

        【讨论】:

          【解决方案5】:

          问题是你应该在循环内部阅读:

          while (num != 0)
          {
              printf("Please type in your number between (and including) 10-100\n");
              scanf("%d", &num);
          
              if((num <10) || (num >100))
              {
                  printf("Error, %d is not allowed\n", num);
              }
              counter++;
          }
          

          您也无需将计数器增加 2 次。

          请注意,这个 sn-p 也会将 counter 递增 0。如果不需要,则除 0 之外的数字的数量是 counter - 1。

          【讨论】:

            【解决方案6】:

            您必须在循环中要求一个数字

            printf("Please type in your number between (and including) 10-100\n");
            scanf("%d", &num);
            

            请检查来自scanf 的返回码以检测错误。

            最后,您在循环内将计数器递增两次。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-05-24
              • 1970-01-01
              • 2021-03-31
              • 2011-07-23
              • 1970-01-01
              相关资源
              最近更新 更多