【问题标题】:Scanf Dont scan char 2nd time while in a loop, any ideas? [duplicate]Scanf 不要在循环中第二次扫描字符,有什么想法吗? [复制]
【发布时间】:2014-11-25 17:00:18
【问题描述】:
    n=10;
    for(i=1;i<n;i++){
        random = 1 + rand() % 3;  
        printf("\n\nIzdari savu izveli ==>");
        scanf("%c", &zime);
        if( zime=='a' )
            printf("\nTu uzliki AKMENI");
        else if( zime=='s' )
            printf("\nTu uzliki SKERES");
         else if( zime=='p' )
            printf("\nTu uzliki PAPIRU");

        if( random==1 )
            printf("\nDators uzlika AKMENI");
        else if( random==2 )
            printf("\nDators uzlika SKERES");
        else if( random==3 )
            printf("\nDators uzlika PAPIRU");

        if( random==1&&zime=='a' || random==2 && zime=='s' || random==3 && zime=='p')
        printf("\n\nNeizskirts!!!");
    else if( random==1&&zime=='p' || random==2&&zime=='a' || random==3&&zime=='s' )
        printf("\n\nTu Uzvareji!!!");
    else if( random==1&&zime=='s' || random==2&&zime=='p' || random==3&&zime=='a' )
        printf("\n\nTu Zaudeji!!!");
    getch();
    }

为什么当循环第二次开始时它只是跳过scanf, 并跳转到if( random==1 ), 然后跳回scanf 让你扫描一个字符?

输出看起来像

这只是石头剪刀布游戏,但有其他语言。

【问题讨论】:

  • 2 分钟前有人问过这个问题!

标签: c


【解决方案1】:

在第一个输入的末尾有一个换行符 \n,它在第二个循环中被 scanf() 选中。

将您的 scanf 更改为:

scanf(" %c", &zime);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2014-10-27
    • 2014-05-24
    • 1970-01-01
    • 2014-03-28
    • 2015-12-15
    • 2015-09-04
    相关资源
    最近更新 更多