求助!求助!解一道c语言题,
#include <stdio.h>int main()
{
int i, PeopleTypes[50];
int baby, child, teen, adult;
baby = child = teen = adult= 0;
printf("请输入1~4的数字,1代表婴儿,2代表儿童,3代表少年,4代表成人,负数终止\n");
for(i=0;i<50;i++)
{
scanf("%d",&PeopleTypes[i]);
if(PeopleTypes[i] < 0) break;
}
for(i=0;i<50;i++)
{
if(PeopleTypes[i] == 1) baby++;
else if(PeopleTypes[i] == 2) child++;
else if(PeopleTypes[i] == 3) teen++;
else if(PeopleTypes[i] == 4) adult++;
else continue;
}
printf("婴儿%d人,儿童%d人,少年%d人,成人%d人\n",baby,child,teen,adult);
return 0;
}
为什么上面这样写最后错了??下面就对了??
#include <stdio.h>
int main()
{
int i, n, PeopleTypes[50];
int baby, child, teen, adult;
baby = child = teen = adult = n = 0;
printf("请输入1~4的数字,1代表婴儿,2代表儿童,3代表少年,4代表成人,负数终止\n");
for(i=0;i<50;i++)
{
scanf("%d",&PeopleTypes[i]);
if(PeopleTypes[i] < 0) break;
n++;
}
for(i=0;i<n;i++)
{
if(PeopleTypes[i] == 1) baby++;
else if(PeopleTypes[i] == 2) child++;
else if(PeopleTypes[i] == 3) teen++;
else if(PeopleTypes[i] == 4) adult++;
else continue;
}
printf("婴儿%d人,儿童%d人,少年%d人,成人%d人\n",baby,child,teen,adult);
return 0;
}