jeezmorty 2019-02-10 21:37 采纳率: 66.7%
浏览 654
已采纳

C语言基础疑问 代码最后的scanf为什么不执行?

这段代码是一个叫simon的游戏,就是输出任意数码 然后消失 然后请用户输入
但是很奇怪编译执行之后,最后一句printf执行了但是scanf却不执行 为什么
编译器是devc++
// a simple simon game
#include
#include
#include
#include
#include
int main(void)
{
char another_game='Y';
const unsigned delay=1;
bool answer=true;
unsigned tries=0;
unsigned digits=0;
time_t seed=0;
time_t wait_start=0;
unsigned number=0;

printf("To play a Simple Simon,watch the screen for a sequence of digits.");
printf("\nWatch carefully, as the digits are only played for %u second%s.",delay, delay==1?"":"s");
printf("\nThe computer will remove them, then prompt you to enter the same sequence.");
printf("\nWhen you do, you must put space in your digits.");
printf("\nGood luck! Press enter to play.\n");

do
{
    answer=true;
    tries=0;
    digits=2;

    while(answer)
    { 
        ++tries;

        srand(time(&seed));//without & the program goes wrong 
        for(unsigned i=1;i<=digits;++i)
            printf("%d ",rand()%10);//output the number

        wait_start=clock();
        for(;clock()-wait_start<delay*CLOCKS_PER_SEC;);
        printf("\r");
        for(unsigned i=1;i<=digits;++i)
            printf("  ");

        printf("\r");
        if(tries==1)
            printf("make sure enter the space between digits!\n");
        srand(seed);
        for(unsigned i=1;i<=digits;++i)
        {
            scanf("%u",&number);//how to make sure the value of seed is stayed
            if(number!=rand()%10)
            {
                answer=false;
            }
        }

        if(answer&&(tries%3==0))
            digits++;

        printf("%s\n",answer?"correct":"wrong");
    }

    printf("\nDo you want to play again(Y/N)?"); 
    scanf("%c",&another_game);

}while(tolower(another_game)=='y'); 

return 0;

}

  • 写回答

2条回答

  • threenewbee 2019-02-11 00:50
    关注

    在scanf前面加上一个
    fflush(stdin);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值