-3

我的任务是使用两种不同的循环(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;


}
4

3 回答 3

1
 while ( num >= 1 && num <= 10 )
    {
            for ( zero = 0; zero <= num; zero++ )
            {
                    printf("%d...", zero);

            }
    }

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

如果您输入“错误”值,那么您将跳过此步骤并进入第二个 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;
}
于 2013-10-10T00:48:36.997 回答
0

将两个 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;);
    }
于 2013-10-10T00:39:42.990 回答
0

真的很简单!

您可以在 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;


}
于 2013-10-11T01:21:50.880 回答