C语言的菜问题~~~

来源:百度知道 编辑:UC知道 时间:2024/05/30 09:05:37
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch;
printf("Give me a letter of the alphabet, and I will give ");
printf("an animal name\nbeginning with that letter.\n");
printf("please type in a letter:type # to end my act.\n");
while((ch=getchar())!='#')
{
if(ch=='\n')
continue;
if(islower(ch))
switch(ch)
{
case 'a':
printf("111:\n");
break;
case 'b':
printf("222:\n");
break;
default:
printf("cuo:\n");
}
else
printf("错啦\n");
while(getchar()!='\n')
continue;
}
printf("Bye:\n");
return 0;
}
/*如果我输入一个非#的小写字母。比如我输入小写a,它应该输出111,我输入c的话
应该到default上,也就是说退出swi

问题是。。你外面还有个更大的hile((ch=getchar())!='#') 呢。。。
你依然在循环中。。

你又没输入#,所以while((ch=getchar())!='#')这个循环始终在执行。

while(getchar()!='\n')
continue;
这个只是把输入流清空。

老大 退出循环的条件是 你输入的是#
也就是输入#后回车才退出循环
而且你的程序混乱不堪 晕

因为你输入a后点了回车也就是'\n',而你没有用一个变量接受'\n'所以下一次循环以为你输入只是'\n'就退出了循环,你可以在输入那行下面加一个c=getchar() "c是一个定义的char"就行了