求C语言题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母

#include<stdio.h>
int main()
{
char week1;
char week2;
printf("please enter a letter\n");
scanf("%c",&week1);
switch(week1)
{
case'M':printf("the answer is Monday\n");
break;
case'W':printf("the answer is Wednesday\n");
break;
case'F':printf("the answer is Friday\n");
break;
case'S':
{
printf("please enter the second letter\n");
scanf("%c",&week2);
if(week2=='a')
printf("the answer is Saturday\n");
else
if(week2=='u')
printf("the answer is Sunday\n");
else
printf("there is no answer\n");
}
break;
case'T':
{
printf("please enter the second letter\n");
scanf("%c",&week2);
if(week2=='u')
printf("the answer is Tuesday\n");
else
if(week2=='h')
printf("the answer is Thursday\n");
else
printf("there is no answer\n");
}
break;
default:
printf("there is no correct answer\n");
break;
}
return 0;
}
在输入第二个字母时出问题?是怎么回事?求指教

在scanf("%c",&week1);

后加一个清除缓冲区;

fflush(stdin)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-20
用scanf时当你输入一个字符过后再输入回车,下次再用scanf时,上次输入的回车会直接被当作字符输入,所以你不能进行第二次输入。像楼上所说,在scanf之后加上fflush(stdin)就可以清空缓冲区了~
第2个回答  推荐于2017-12-15
在 scanf("%c",&week1); 下加一句:
getchar();追问

亲,能解释一下为什么吗?

追答

输入一个字符加回车,scanf("%c",&week1);只能接收一个字符,回车还留在输入流中,被scanf("%c",&week2);接收。加getchar()可以接收这个回车,这样到scanf("%c",&week2);时就可以重新输入了。楼下的也可以。

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网