求C语言题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母
#include<stdio.h>intmain(){charweek1;charweek2;printf("pleaseenteraletter\n");scanf("...
#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;
}
在输入第二个字母时出问题?是怎么回事?求指教 展开
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;
}
在输入第二个字母时出问题?是怎么回事?求指教 展开
3个回答
展开全部
用scanf时当你输入一个字符过后再输入回车,下次再用scanf时,上次输入的回车会直接被当作字符输入,所以你不能进行第二次输入。像楼上所说,在scanf之后加上fflush(stdin)就可以清空缓冲区了~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在 scanf("%c",&week1); 下加一句:
getchar();
getchar();
追问
亲,能解释一下为什么吗?
追答
输入一个字符加回车,scanf("%c",&week1);只能接收一个字符,回车还留在输入流中,被scanf("%c",&week2);接收。加getchar()可以接收这个回车,这样到scanf("%c",&week2);时就可以重新输入了。楼下的也可以。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询