#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;
}
在输入第二个字母时出问题?是怎么回事?求指教
getchar();追问
亲,能解释一下为什么吗?
追答输入一个字符加回车,scanf("%c",&week1);只能接收一个字符,回车还留在输入流中,被scanf("%c",&week2);接收。加getchar()可以接收这个回车,这样到scanf("%c",&week2);时就可以重新输入了。楼下的也可以。
本回答被提问者采纳