看一个 continue的程序
c prim plus 7.9
1 #include<stdio.h> 2 #include<ctype.h> 3 int main(void) 4 { char ch; 5 printf(" You give me a letter, and I give you a word; " 6 " Input a # to quit "); 7 while((ch=getchar())!='#') 8 { 9 if(' ' == ch) continue; //处理刚开始输入的换行符 10 11 if(islower(ch)) 12 switch(ch) 13 { 14 case 'a': printf("apple "); break; 15 case 'b': printf("basketball "); break; 16 case 'c': printf("cell "); break; 17 case 'd': printf("desirable "); break; 18 case 'e': printf("elements "); break; 19 case 'f' :printf("fisher, brownish marten ");break; 20 default: printf("this is a beautiful world! "); break; 21 } 22 else 23 printf("Input a lowercase letter. "); 24 25 /*读取并丢弃输入,直至一行的末尾*/ 26 while( getchar()!=' ') continue; //跳过输入行的剩余部分 27 28 printf("Input another letter "); 29 printf("---------------------- "); 30 31 } 32 printf("Bye···· "); 33 return 0; 34 }
实验结果如下:
范的错误,我第9行
if('
' == ch) continue; 刚开始写为了 if('
' == getchar()) continue; 这样呢,比如输入 两个字符,才能进入switch···呜呜呜····以后注意哦···