利用键盘随机输入若干个学生成绩(0-100分),然后分别统计0-59,60-79,80-100之间各有多少名学生。写出相应的程序代码。
随机输入若干个学生成绩(0-100分)
输出0-59,60-79,80-100之间各有多少名学生,结果以空格分割
提示
本题一直读到文件末尾
输入文件结束后有自动结束输入的文件结束符。
要想在小黑窗看到结果,数据输入结束后换行再输入ctrl+z模拟文件结束符。
#include <stdio.h>
int main()
{
int score;
int a = 0,b=0,c=0,d=0,e=0;
while(scanf("%d",&score) != EOF)
{
switch(score/10)
{
case 10:
case 9:
a++;
break;
case 8:
b++;
break;
case 7:
c++;
break;
case 6:
d++;
break;
default:
e++;
break;
}
}
printf("%d %d %d %d %d",a,b,c,d,e);
return 0;
}
建议先亲自尝试写一下代码,哪怕不正确也不要紧,贴出自己尝试的代码和错误,然后别人可以帮你分析讨论。
#include <stdio.h>
{
int n;
int x1 = 0, x2 = 0, x3 = 0;
printf("请输入学生的成绩1~100:\n");
scanf("%d", &n);
while (n != -1)
{
if (0 <= n && n <= 59)
{
x1 += 1;
}
else if (60 <= n && n <= 79)
{
x2 += 1;
}
else if (80 <= n && n <= 100)
{
x3 += 1;
}
scanf("%d", &n);
}
printf("1~59分数段的人数为:%d\n", x1);
printf("60~79分数段的人数为:%d\n", x2);
printf("80~100分数段的人数为:%d\n", x3);
}