Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-循环不';我不能继续_C_Loops - Fatal编程技术网

C-循环不';我不能继续

C-循环不';我不能继续,c,loops,C,Loops,为什么在我将字母y输入char答案后,这个循环不继续 我原以为getchar()会有帮助,但它似乎什么都没做 #include <stdio.h> int main(void) { char answer = 'y'; int num = 0; printf("Enter a number: \n"); scanf("%d", &num); while(answer != 'n') { int mult =

为什么在我将字母
y
输入
char
答案后,这个循环不继续

我原以为
getchar()
会有帮助,但它似乎什么都没做

#include <stdio.h>

int main(void)
{
    char answer = 'y';
    int num = 0;
    printf("Enter a number: \n");
    scanf("%d", &num);

    while(answer != 'n')
    {
        int mult = 1;
        int k = 1;
        while (k <= num)
        {
            mult *= k;
            k++;
        }
        printf("%d! = %d\n", num, mult);

        printf("Would you like to try another number? \n");
        printf("Enter: y for yes | n for no\n");
        getchar();
        scanf("%c", &answer);
    }       
}
#包括
内部主(空)
{
char-answer='y';
int num=0;
printf(“输入一个数字:\n”);
scanf(“%d”和&num);
while(回答!=“n”)
{
int mult=1;
int k=1;
while(k
使用
c=getchar();
并省略
scanf


还请注意,更惯用的方法是使用
do while
循环。

我在您的程序中做了一些更正

  • 很难理解你的程序是做什么的。我通过为
    factorial
    定义一个单独的可重用函数,使你的程序更具可读性和模块化
  • 如果用户输入了
    'y'
    ,再次向用户询问号码更有意义。因此,我移动了所需的代码
  • 我不建议在程序中混合使用
    scanf
    getchar
    。因此,我们可以在程序中使用
    scanf
    scanf()
    函数在尝试解析字符以外的转换之前自动删除空白。字符格式(主要是%c;还可以扫描设置%[…]和%n)是例外;它们不删除空格
  • 将“%c”与
    前导空格一起使用可跳过可选空格。请勿在scanf()格式字符串中使用尾随空格

    请注意,这仍然不会消耗输入流中留下的任何尾随空格,甚至不会消耗到行尾,因此如果在同一输入流上使用getchar()或fgets(),请注意这一点。我们只是让scanf在转换之前跳过空格,就像对%d和其他非字符转换一样

    #include <stdio.h>
    
    
    unsigned long factorial(unsigned long num)
    {
        unsigned long mult = 1;
        unsigned long k = 1;
        while (k <= num) {
            mult *= k;
            k++;
        }
    
        return mult;
    }
    
    int main(void)
    {
        char answer = 'y';
    
        do {
            int num = 0;
            printf("Enter a number: \n");
            scanf(" %d", &num);
    
    
            unsigned long mult = factorial(num);
            printf("%d! = %lu\n", num, mult);
    
            printf("Would you like to try another number? \n");
            printf("Enter: y for yes | n for no\n");
            scanf(" %c", &answer);
    
        } while (answer != 'n');  
    
        return 0;   
    }   
    
    #包括
    无符号长阶乘(无符号长数值)
    {
    无符号长mult=1;
    无符号长k=1;
    
    while(k您的代码工作并且循环继续:它不会重新请求数字,因为该部分在循环之外。将其移到内部。还要注意,您的代码即使在按下任何其他键而不是
    n
    时也会继续执行

    这是一种从代码开始的方法:

        #include <stdio.h>
    
        int main(void)
        {
                char answer = 'y';
                int num = 0;
    
                while(answer != 'n')
                {
                        printf("Enter a number: \n");
                        scanf("%d", &num);
                        int mult = 1;
                        int k = 1;
                        while (k <= num)
                        {
                                mult *= k;
                                k++;
                        }
                        printf("%d! = %d\n", num, mult);
    
                        printf("Would you like to try another number? \n");
                        printf("Enter: any key for  yes | n for no\n");
                        getchar();
                        scanf("%c", &answer);
                }
        }
    
    #包括
    内部主(空)
    {
    char-answer='y';
    int num=0;
    while(回答!=“n”)
    {
    printf(“输入一个数字:\n”);
    scanf(“%d”和&num);
    int mult=1;
    int k=1;
    
    while(k)如果您希望循环返回到
    printf(“输入一个数字:\n”)
    -部分,请注意这部分在循环之外。您实际观察到了什么?您希望看到什么?如果您希望跳过字符前的前导空格,请使用带空格的格式字符串。如
    %c
    。还要注意,在循环条件下,除了小写的
    'n'
    之外,任何东西都将继续循环,不仅是
    'y'
    。这也是一个非常好的时间。在调试器中逐行检查代码,检查所有变量的值(并将
    getchar
    的结果保存在一个伪变量中,以便您也可以进行检查).我刚刚测试了你的代码,如果你想在写“n”时停止,在写“y”时继续,那么它具有预期的行为,因此我在第一次迭代中没有看到你的问题,第一次迭代将读取第一次调用
    scanf
    时留下的换行符。
        #include <stdio.h>
    
        int main(void)
        {
                char answer = 'y';
                int num = 0;
    
                while(answer != 'n')
                {
                        printf("Enter a number: \n");
                        scanf("%d", &num);
                        int mult = 1;
                        int k = 1;
                        while (k <= num)
                        {
                                mult *= k;
                                k++;
                        }
                        printf("%d! = %d\n", num, mult);
    
                        printf("Would you like to try another number? \n");
                        printf("Enter: any key for  yes | n for no\n");
                        getchar();
                        scanf("%c", &answer);
                }
        }