更新时间2018-03-06 19:45:54
为什下面这个程序运行时输入a,按下【enter】没动静,但是再按下【enter】后显示I recognize only lowercase letters. 怎么才能让程序按照我的语气执行case a中的语句呢?#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 beginning with that letter. "); printf("please type in a letter;type to end my act. "); while(ch=getchar()!='#') { if(' '==ch) continue; if(islower(ch)) switch(ch) { case 'a': printf("argali,a wild shhep of Asia "); break; case 'b': printf("babirusa,a wild pig of Malay "); break; case 'c': printf("coati,racoonlike mamal "); break; case 'd': printf("desman,aquatic,molelike critter "); break; case 'e': printf("echidina,the spiny anteater "); break; case 'f': printf("fisher,brownish marten "); default: printf("That's a stumper"); }//swich结构结束,但while循环仍在继续 else printf("I recognize only lowercase letters. "); while(getchar()!=' ') continue; }//while循环结束 printf("Bye! "); return 0; }
你的
while(ch=getchar()!='#')
不正确(注意运算符优先级)
正确的
while((ch=getchar())!='#')