注册 登录
编程论坛 新人交流区

求助:switch问题

lonelyfree 发布于 2007-10-24 22:41, 322 次点击

#include <stdio.h>

void main()
{
char letter;

int vowel_count = 0;
int consonant_count = 0;

for (letter = 'A'; letter <= 'Z'; letter++)
switch (letter) {
case 'A':
case 'E':
case 'I':
case 'O':vowel_count++;
break;
case 'U':
default: consonant_count++;
};

printf("The number of vowels is %d\n", vowel_count);
printf("The number of vowels is %d\n", consonant_count);
}
运行结果:The number of vowels is 4
The number of vowels is 22

请问 第一个结果“4”是怎么得出的?
case‘o’应该只执行了一次就跳出了switch了,为什么vowel_count的值为4了?

[此贴子已经被作者于2007-10-24 22:49:58编辑过]

4 回复
#2
chengbo2262007-10-25 12:19
switch中
letter='A';
letter='E';
letter='I';
letter='O';
时,vowel_count都执行了一次后加运算,所以结果是4.
#3
lonelyfree2007-10-25 13:17
回复:(chengbo226)switch中letter=\'A\';letter=\'E\';...
问题已经解决,谢谢~!!
#4
petroe2007-10-25 14:19
我也是新人,还要灌
#5
lirj7702007-10-25 15:12
继续灌,不到
1