c语言 统计各个分数段的学生人数

统计各个分数段的学生人数。一个一个地输入学生的成绩,最后以-1表示结束,统计并输出各个分数段的学生人数。分数段1-59;60-69;70-79;80-89;90-100.用while循环来做

#include <stdio.h> //使用printf函数要包含的头文件void main(void) //主函数
{
int n;
int x1=0,x2=0,x3=0,x4=0,x5=0;
printf("请输入学生的成绩1~100:\n");
scanf("%d",&n);
while(n!=-1)
{
if(1<=n&&n<=59)
{
x1+=1;
}
else if(60<=n&&n<=69)
{
x2+=1;
}
else if(70<=n&&n<=79)
{
x3+=1;
}
else if(80<=n&&n<=89)
{
x4+=1;
}
else
{
x5=+1;
}
scanf("%d",&n);
}
printf("1~59分数段的人数为:%d\n",x1);
printf("60~69分数段的人数为:%d\n",x2);
printf("70~79分数段的人数为:%d\n",x3);
printf("80~89分数段的人数为:%d\n",x4);
printf("90~100分数段的人数为:%d\n",x5);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-05-07
#include<stdio.h>
int count[5];
const char *str[5] = {"1-59: ","60-69: ","70-79: ","80-89: ","90-100: "};
int main(void)
{
int score,i;
while(1)
{
scanf("%d",&score);
if(score==-1)
break;
if(score>=1 && score <=59)
count[0]++;
else if(score >= 60 && score<=69)
count[1]++;
else if(score>=70 && score<=79)
count[2]++;
else if(score>=80 && score<=89)
count[3]++;
else if(score>=90 && score<=100)
count[4]++;
else
printf("输入错误");
}

for(i = 0; i < 5; i++)
{
printf("%s%d\n",str[i],count[i]);
}
return 0;
}

第2个回答  2019-08-25
int main(void)
{
int a[6], i, cj,cj2,cj3,cj4,cj5,n;
scanf("%d", &a[0]);
n = 0;
while (a[n]!=-1)
{
n++;
scanf("%d", &a[n]);
}
cj = 0, cj2 = 0, cj3 = 0, cj4 = 0,cj5=0;
for (i = 0; i < 6-1; i++)
{
switch (a[i] / 10)
{
case 10:
case 9:cj++; break;
case 8:cj2++; break;
case 7:cj3++; break;
case 6:cj4++; break;
default:cj5++; break;
}
}
printf("<60:%d\n60~69:%d\n70~79:%d\n80~89:%d\n90~100:%d\n",cj5,cj4,cj3,cj2,cj);
return 0;
}

第3个回答  2018-05-07
运行有误,且应当加个void main()!