为什么我的 C 代码会重复第一行 printf?

问题描述 投票:0回答:3

我想要一个无限循环,这很有效。但我不知道为什么它两次打印出同一行。有什么想法吗?

输出:

最后选择:p
你的下一步:RL最后的选择:最后的选择:l
最后选择: 最后选择: r
你的下一步:SL最后的选择:最后的选择:

到目前为止这是我的代码:

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

int main ()
{
    while (true)
    {
// Prompt user for input last choice contender
        char c;
        printf("Last choice: ");
        c = getchar();
      // If last choice is R, loser should play S
        if (c == 'R'|| c == 'r')
        {
            printf("Your next move: S");
        }
// Else if last choice is P, loser should play R
        else if (c == 'P' || c == 'p')
        {
            printf("Your next move: R");
        }
// Else if last choice is S, loser should play P
        else if (c == 'S' || c == 's')
        {
            printf("Your next move: P");
        }
    }
}
c while-loop printf cs50
3个回答
0
投票

这是因为当您输入密钥然后按回车键时,getchar 会正确获取您的密钥,并且还会得到一个

\n
,这是您的回车键。所以你只需要在你的代码中添加一点 if 就可以让它像这样工作:

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

int main ()
{
    while (true)
    {
// Prompt user for input last choice contender
        char c;
        printf("Last choice: ");
        c = getchar();
        if (c != '\n')
            getchar(); //gets the \n if you've given a letter
      // If last choice is R, loser should play S
        if (c == 'R'|| c == 'r')
        {
            printf("Your next move: S");
        }
// Else if last choice is P, loser should play R
        else if (c == 'P' || c == 'p')
        {
            printf("Your next move: R");
        }
// Else if last choice is S, loser should play P
        else if (c == 'S' || c == 's')
        {
            printf("Your next move: P");
        }
    }
}

0
投票

我想我已经为你工作了!

#include <stdio.h>
int main () {
    printf("Last choice: ");
    while (1) {
        char c;
        c = getchar();
        if(c != '\n')
        {
            if (c == 'R'|| c == 'r') {
                printf("Your next move: S\n");
            } else if (c == 'P' || c == 'p') {
                printf("Your next move: R\n");
            } else if (c == 'S' || c == 's') {
                printf("Your next move: P\n");
            } else {
                printf("Use valid input\n");
            }
            printf("Last choice: ");
        }
    }
}

0
投票

希望您能得到答案,如果没有,请让我帮助您。 看到您正在使用 getchar 它从输入缓冲区读取字符,但是,当 while 重新读取时,它会读取两次缓冲区中剩余的字符,而其他它会自动读取新行。 为了解决这个问题,我们可以使用 fflush(stdout) 这会刷新输出缓冲区,而 flush(stdin) 在读取输入之前刷新输入缓冲区。 如何在您的代码中使用它们? 就在 printf("last choice") 之后和 c=getchar() 之前,如代码所示

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

int main ()
{
    while (true)
    {
// Prompt user for input last choice contender
        char c;
        printf("Last choice: ");
         fflush(stdout);
         fflush(stdin);
        c = getchar();
   // If last choice is R, loser should play S
        if (c == 'R'|| c == 'r')
        {
            printf("Your next move: S");
        }
// Else if last choice is P, loser should play R
        else if (c == 'P' || c == 'p')
        {
            printf("Your next move: R");
        }
// Else if last choice is S, loser should play P
        else if (c == 'S' || c == 's')
        {
            printf("Your next move: P");
        }
    }
}
 

希望这有帮助,请告诉我!

© www.soinside.com 2019 - 2024. All rights reserved.