C语言,根据输入的字母判断是星期几
#include<stdio.h>voidmain(){printf("pleaseenterthefirstletterofsomeday:\n");charlette...
#include <stdio.h>
void main()
{
printf("please enter the first letter of someday:\n");
char letter;
while((letter=getchar())!='y')
{switch(letter){
case 'S':
{printf("please enter the second letter:");
if((letter=getchar())=='a')
printf("It is Saturday!\n");
else if((letter=getchar())=='u')
printf("It is Sunday!\n");
else printf("error\n");
break;
}
case 'M':printf("It is Monday!\n");
break;
case 'T':{
printf("please enter the second letter:");
if((letter=getchar())=='h')
printf("It is Tuesday!\n");
else if((letter=getchar())=='u')
printf("It is Thursday!\n");
break;
}
case 'F':printf("It is Friday!\n");
break;
case 'W':printf("It is Wednesday!\n");
break;
}
}
}
现在遇到问题,比如说输入“S”,提示输入第二个字母,如果输入“u”,就输出Sunday,但如果输入“a”,就没有任何反应。 展开
void main()
{
printf("please enter the first letter of someday:\n");
char letter;
while((letter=getchar())!='y')
{switch(letter){
case 'S':
{printf("please enter the second letter:");
if((letter=getchar())=='a')
printf("It is Saturday!\n");
else if((letter=getchar())=='u')
printf("It is Sunday!\n");
else printf("error\n");
break;
}
case 'M':printf("It is Monday!\n");
break;
case 'T':{
printf("please enter the second letter:");
if((letter=getchar())=='h')
printf("It is Tuesday!\n");
else if((letter=getchar())=='u')
printf("It is Thursday!\n");
break;
}
case 'F':printf("It is Friday!\n");
break;
case 'W':printf("It is Wednesday!\n");
break;
}
}
}
现在遇到问题,比如说输入“S”,提示输入第二个字母,如果输入“u”,就输出Sunday,但如果输入“a”,就没有任何反应。 展开
3个回答
展开全部
你输入了S后的回车还在缓冲区中,后来被读取到letter中了,而不是你输入的字母,所以才会出错,你在之前再加一个getchar就行了。还有你的第二个else if中不需要再读取字母了,直接判断就行了。下面是整理后的代码。我建议你直接读取一段作为字符串来判断比较方便,这样比较麻烦。
#include <stdio.h>
void main()
{
printf("please enter the first letter of someday:\n");
char letter;
while ((letter = getchar()) != 'y') {
switch (letter) {
case 'S': {
printf("please enter the second letter:");
getchar();
if ((letter = getchar()) == 'a') {
printf("It is Saturday!\n");
} else if (letter == 'u') {
printf("It is Sunday!\n");
} else {
printf("error\n");
}
break;
}
case 'M':
printf("It is Monday!\n");
break;
case 'T': {
printf("please enter the second letter:");
getchar();
if ((letter = getchar()) == 'h') {
printf("It is Tuesday!\n");
} else if (letter == 'u') {
printf("It is Thursday!\n");
}
break;
}
case 'F':
printf("It is Friday!\n");
break;
case 'W':
printf("It is Wednesday!\n");
break;
}
}
}
#include <stdio.h>
void main()
{
printf("please enter the first letter of someday:\n");
char letter;
while ((letter = getchar()) != 'y') {
switch (letter) {
case 'S': {
printf("please enter the second letter:");
getchar();
if ((letter = getchar()) == 'a') {
printf("It is Saturday!\n");
} else if (letter == 'u') {
printf("It is Sunday!\n");
} else {
printf("error\n");
}
break;
}
case 'M':
printf("It is Monday!\n");
break;
case 'T': {
printf("please enter the second letter:");
getchar();
if ((letter = getchar()) == 'h') {
printf("It is Tuesday!\n");
} else if (letter == 'u') {
printf("It is Thursday!\n");
}
break;
}
case 'F':
printf("It is Friday!\n");
break;
case 'W':
printf("It is Wednesday!\n");
break;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询