应用switch 语句,
1 #include2 int main(void) 3 { char ch; 4 int a_ctl,e_ctl,i_ctl,o_ctl,u_ctl; 5 a_ctl = e_ctl = i_ctl = o_ctl = u_ctl =0; 6 printf(" Enter some text,and a # to quit\n" 7 " I will tell you the number of the vowel\n" ); 8 while((ch=getchar()) != '#') 9 {10 switch(ch)11 {12 case 'A':13 case 'a': a_ctl++;break;14 case 'E':15 case 'e': e_ctl++;break;16 case 'I':17 case 'i': i_ctl++;break;18 case 'O':19 case 'o': o_ctl++;break;20 case 'U':21 case 'u': u_ctl++;break;// 保留此处的break,为了方便以后添加其他选项 22 default: break;23 }24 25 }26 printf("number of vowels: \n"27 "a:%d e:%d i:%d o:%d u:%d\n",a_ctl,e_ctl,i_ctl,o_ctl,u_ctl);28 return 0;29 }
刚开始 把printf放在 while循环里面了,结果就悲剧了·····以后注意哈