酒话醉人丶 2016-10-18 10:28 采纳率: 0%
浏览 2317

新人求教c语言问题,出现switch函数没有起作用,直接跳到下一步

部分代码如下,在Choose函数中:想使用一个循环,结果在第一个那个部分进行选择时出现错误,
选择Choicepass()函数后,Choicepass()函数仅仅执行到"scanf("%c",&M);",
再直接进入Choose函数中的" printf("是否重新进行?\nc->继续\ne->退出:");"这条语句。
想要知道为什么,求大神为新手求解,万分感谢!
结果如图:
图片说明
int Choicepass()

{
int n,T,L,S1,S2,S3;
char M;
printf("Please choose the model:\na->easy\nb->normal\nc->difficulty\n:");
scanf("%c",&M);

switch(M)                                                        //选择难度所用函数
{
case 'a':
    {
        srand(time(0));
        n = rand()%10+1;
        T = 3;
        S1 = Guess1(n,T);
    }break;
case 'b':
    {
        srand(time(0));
        n = rand()%50+1;
        printf("You should get the answer from 1~50.\nPlease choose the TimesLimit(5~10):");
        scanf("%d",&T);
        S2 = Guess2(n,T);
    }break;
case 'c':
    {
        srand(time(0));
        n = rand()%100+1;
        printf("You should get the answer from 1~100.\nPlease choose the TimesLimit(5~15):");
        scanf("%d",&T);
        S3 = Guess3(n,T);
    }break;
}
L = 1;
return L ;

}

    /*选择关卡模式或者选择难度模式*/

int Choose()
{
int L,T;
char k,h;

printf("选择关卡模式或者自由选择难度模式:\ng->关卡\nz->自由:");
scanf("%c",&h);

if(h == 'c')

    L = Customspass();

else if(h == 'z')

    L = Choicepass();

else printf("ERROR!\n");

printf("是否重新进行?\nc->继续\ne->退出:");
scanf("%c",&k);

if(k == 'c')     

    T = Choose();

else     
    printf("游戏结束!\n");

return T;

}

  • 写回答

1条回答 默认 最新

  • hander_left 2016-10-18 11:26
    关注

    没太看懂你的问题。如果我没有理解错的话,应该是缓冲区问题。
    在choose()函数中的scanf(“%c”,&h)后面加一句getchar()试试

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘