#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('\n' == ch)
continue;
if (islower(ch)) /* lowercase only */
switch (ch)
{
case 'a' :
printf("argali, a wild sheep of Asia\n");
break;
case 'b' :
printf("babirusa, a wild pig of Malay\n");
break;
case 'c' :
printf("coati, racoonlike mammal\n");
break;
case 'd' :
printf("desman, aquatic, molelike critter\n");
break;
case 'e' :
printf("echidna, the spiny anteater\n");
break;
case 'f' :
printf("fisher, brownish marten\n");
break;
default :
printf("That's a stumper!\n");
} /* end of switch */
else
printf("I recognize only lowercase letters.\n");
while (getchar() != '\n')
continue; /* skip rest of input line */
printf("Please type another letter or a #.\n");
} /* while loop end */
printf("Bye!\n");
return 0;
}
这个while循环刚开始的第一个if
if('\n' == ch)
continue;
这个有什么用
然后循环最后的这个while内循环
while (getchar() != '\n')
continue;
书上说是跳过输入行的剩余部分是什么意思
if('\n' == ch)
continue; 跳过你每次输入之前的无效换行符号
while (getchar() != '\n')
continue; 效果就是每次输入只会读取你的第一个符号无论输入多长
continue就是本次循环截止,进行下一次循环
就是一直读入字符串,当遇到空格时跳出循环,不在读取剩下的字符串
continue是什么意思,知道吧?
break是整个跳出循环,类似函数里的return一样,只不过跳出的不是函数而是循环体
continue则是跳出后不直接跑到外面,而是回到开头继续执行。
基本语法必须先理解了,才能继续往下讲。
-=-=-==-
if('\n' == ch)continue;//当输入是换行时,跳过,回到开头,继续读下一个字符,这能理解吧,
while (getchar() != '\n')continue;//当进入循环之后,就一直读字符,只要读到的字符不是换行,就执行continue,但是continue是唯一的代码,并没有任何代码可供跳过,所以这个代码就等价于while (getchar() != '\n');总之就是不停读字符进来,直到读到一个换行为止,循环结束