下面是一個(gè)簡(jiǎn)單的c語(yǔ)言程序,,但運(yùn)行時(shí)出了問題,,具體描述在下方。懇請(qǐng)各位大佬幫忙解答一下,。
為什下面這個(gè)程序運(yùn)行時(shí)輸入a,,按下【enter】沒動(dòng)靜,,但是再按下【enter】后顯示I recognize only lowercase letters. 怎么才能讓程序按照我的語(yǔ)氣執(zhí)行case a中的語(yǔ)句呢?#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 namebeginning 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結(jié)構(gòu)結(jié)束,,但while循環(huán)仍在繼續(xù)elseprintf("I recognize only lowercase letters.");while(getchar()!='')continue;}//while循環(huán)結(jié)束printf("Bye!");return 0;}
你的
while(ch=getchar()!='#')
不正確(注意運(yùn)算符優(yōu)先級(jí))
正確的
while((ch=getchar())!='#')