【问题标题】:Why won't my program stop looping?为什么我的程序不会停止循环?
【发布时间】:2013-10-17 13:40:19
【问题描述】:

我的任务是使用两种不同的循环(For、While、do While)。任务是要求用户输入一个介于 1 和 10 之间的数字,然后程序将从 0 数到用户数。此外,如果用户输入了 1 到 10 之外的数字,程序必须能够显示错误消息并要求用户再次输入数字。带有错误消息的代码部分和再次输入数字的提示工作得很好。但是,当我在该范围内输入一个数字时,它要么什么都不做,要么从 0 数到它们的数字无限次,并且不会停止循环计数。请帮忙!

#include <stdio.h>

int main(void)

{
    //Variables
    int num;
    int zero;

    //Explains to the user what the program will do
    printf("This program will count from 0 to a number you pick.\n\n");

    //Asks the user to input a value
    printf("Please enter a number (between 1 and 10): \n");
    scanf("%d", &num);


    //If the correct range was selected by the user
    while ( num >= 1 && num <= 10 )
    {
            for ( zero = 0; zero <= num; zero++ )
            {
                    printf("%d...", zero);

            }
    }

    //If a value outside of the accepted range is entered
    while ( num < 1 || num > 10)
    {
            printf("I'm sorry, that is incorrect.\n");
            printf("Please enter a number (between 1 and 10): \n");
            scanf("%d", &num);
    }


    printf("\n\n\n");

    return 0;


}

【问题讨论】:

  • 你需要展示你的程序。
  • 欢迎来到 StackOverflow。基于对问题的一些模糊描述,我们无法为您提供我们看不到的代码。请访问SSCCE,然后回到这里edit您的问题并提供您的相关代码的SSCCE(不是大代码转储),对问题的清晰描述,并询问基于该代码的特定问题,也许我们可以帮助您解决问题。
  • 提供一些示例代码。它不需要工作,但要表现出努力。您将需要声明几个变量,打印提示,接受输入,将其转换为数字,然后循环该数字。试一试。

标签: c loops for-loop while-loop putty


【解决方案1】:
 while ( num >= 1 && num <= 10 )
    {
            for ( zero = 0; zero <= num; zero++ )
            {
                    printf("%d...", zero);

            }
    }

如果 num 介于 1 和 10 之间,它将永远运行,因为 num 在循环内不会更改 - 一旦进入,你就永远进入了。

如果您输入“错误”值,那么您将跳过此步骤并进入第二个 while 循环。然而,一旦你通过输入一个“好”的值退出那个 while 循环,剩下要做的就是

printf("\n\n\n");

return 0;

您需要摆脱第一个 while 循环并将第二个循环移到 for 循环之上:

#include <stdio.h>

int main(void)

{
    //Variables
    int num;
    int zero;

    //Explains to the user what the program will do
    printf("This program will count from 0 to a number you pick.\n\n");

    //Asks the user to input a value
    printf("Please enter a number (between 1 and 10): \n");
    scanf("%d", &num);

    //If a value outside of the accepted range is entered
    while ( num < 1 || num > 10)
    {
            printf("I'm sorry, that is incorrect.\n");
            printf("Please enter a number (between 1 and 10): \n");
            scanf("%d", &num);
    }

   for ( zero = 0; zero <= num; zero++ )
   {
            printf("%d...", zero);

   }

    printf("\n\n\n");

    return 0;
}

【讨论】:

    【解决方案2】:

    将你的两个 while 循环更改为 if 守卫,

        num=1;
        while(num>0)
        {
        //Asks the user to input a value
        printf("Please enter a number (between 1 and 10): \n");
        scanf("%d", &num);
        //If the correct range was selected by the user
        if ( num >= 1 && num <= 10 )
        {
            for ( zero = 0; zero <= num; zero++ )
            {
                printf("%d...", zero);
            }
        }
        //If a value outside of the accepted range is entered
        if ( num < 1 || num > 10)
        {
                printf("I'm sorry, that is incorrect.\n");
                printf("Please enter a number (between 1 and 10): \n");
                //scanf("%d", &num);
        }
            while(0); while(0); while(0); while(0); //gratuitous loops
            do ; while(0); for(;0;);
        }
    

    【讨论】:

    • 可以,但是我的导师需要两种不同类型的循环。那么,如果我用 'if' 语句替换 'while' 循环,我会将另一个循环放在哪里?
    • 注意 while(num>0) 外循环和 for(...) 内循环。完成。
    • 你的while循环破坏了一些东西。如果要添加无关的 while 循环,请在任何地方添加:while(0);
    【解决方案3】:

    真的很简单!

    您可以在 while 循环中使用 break

    #include <stdio.h>
    
    int main(void)
    
    {
    //Variables
    int num;
    int zero;
    
    //Explains to the user what the program will do
    printf("This program will count from 0 to a number you pick.\n\n");
    
    //Asks the user to input a value
    printf("Please enter a number (between 1 and 10): \n");
    scanf("%d", &num);
    
    
    //If the correct range was selected by the user
    while ( num >= 1 && num <= 10 )
    {
            for ( zero = 0; zero <= num; zero++ )
            {
                    printf("%d...", zero);
    
            }
            break; // !!! Here !!!
    }
    
    //If a value outside of the accepted range is entered
    while ( num < 1 || num > 10)
    {
            printf("I'm sorry, that is incorrect.\n");
            printf("Please enter a number (between 1 and 10): \n");
            scanf("%d", &num);
    }
    
    
    printf("\n\n\n");
    
    return 0;
    
    
    }
    

    【讨论】:

    • 你不能用 if 语句代替 while + break 吗?
    猜你喜欢
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多