C语言程序中for循环和switch的用法?
每个case后面该加什么?到这里卡住了#include<stdio.h>intmain(){inti,j,num[5],p;charorder;floatscore[5]...
每个case后面该加什么?到这里卡住了
# include<stdio.h>
int main()
{int i,j,num[5],p;char order;
float score[5],sum=0.0,average,t,u;
for(i=0;i<5;i++)
{printf("请输入学号:\n"); scanf("%d",&num[i]);
printf("请输入成绩:\n"); scanf("%f",&score[i]);
sum=sum+score[i];}
for(i=0;i<5;i++) //输出大于等于80分的学号成绩//
{if(score[i]>=80)
printf("学号:%d成绩:%5.2f\n",num[i],score[i]);}
for(i=0;i<5;i++) //依次输出学号成绩//
{printf("%d,%7.2f",num[i],score[i]);
printf("\n");}
for(j=0;j<4;j++) //成绩由高到低排序//
for(i=0;i<4-j;i++)
if(score[i]<score[i+1])
{t=score[i+1];score[i+1]=score[i];score[i]=t;
u=num[i+1];num[i+1]=num[i];num[i]=u;}
for(i=0;i<5;i++)
printf("%d,%5.2f\n",num[i],score[i]);printf("\n");
average=sum/5.0; //平均成绩//
printf("全班平均成绩为%7.2f\n",average);
scanf("%c",&order);
switch(order)
{case'A':
case'B':
case'C':
case'D':
default:printf("Error\n");}
return 0;
} 展开
# include<stdio.h>
int main()
{int i,j,num[5],p;char order;
float score[5],sum=0.0,average,t,u;
for(i=0;i<5;i++)
{printf("请输入学号:\n"); scanf("%d",&num[i]);
printf("请输入成绩:\n"); scanf("%f",&score[i]);
sum=sum+score[i];}
for(i=0;i<5;i++) //输出大于等于80分的学号成绩//
{if(score[i]>=80)
printf("学号:%d成绩:%5.2f\n",num[i],score[i]);}
for(i=0;i<5;i++) //依次输出学号成绩//
{printf("%d,%7.2f",num[i],score[i]);
printf("\n");}
for(j=0;j<4;j++) //成绩由高到低排序//
for(i=0;i<4-j;i++)
if(score[i]<score[i+1])
{t=score[i+1];score[i+1]=score[i];score[i]=t;
u=num[i+1];num[i+1]=num[i];num[i]=u;}
for(i=0;i<5;i++)
printf("%d,%5.2f\n",num[i],score[i]);printf("\n");
average=sum/5.0; //平均成绩//
printf("全班平均成绩为%7.2f\n",average);
scanf("%c",&order);
switch(order)
{case'A':
case'B':
case'C':
case'D':
default:printf("Error\n");}
return 0;
} 展开
1个回答
推荐于2016-06-02
展开全部
# include<stdio.h>
int i, j, num[5], p;
char order;
float score[5], sum = 0.0, average, t, u;
void PrintAll() {
for (i = 0; i < 5; i++) { // 依次输出学号成绩//
printf("%d,%7.2f", num[i], score[i]);
printf("\n");
}
}
void ComputeAverage() {
average = sum / 5.0; // 平均成绩//
printf("全班平均成绩为%7.2f\n", average);
}
void Sort() {
for (j = 0; j < 4; j++) // 成绩由高到低排序//
for (i = 0; i < 4 - j; i++)
if (score[i] < score[i + 1]) {
t = score[i + 1];
score[i + 1] = score[i];
score[i] = t;
u = num[i + 1];
num[i + 1] = num[i];
num[i] = u;
}
for (i = 0; i < 5; i++)
printf("%d,%5.2f\n", num[i], score[i]);
}
void PrintCxcellent() {
for (i = 0; i < 5; i++) { // 输出大于等于80分的学号成绩
if (score[i] >= 80)
printf("学号:%d成绩:%5.2f\n", num[i], score[i]);
}
}
char Menu() {
printf("A:输出全班学生的学号和成绩\n");
printf("B:计算并输出全班的平均成绩\n");
printf("C:降序并输出全班学号与成绩\n");
printf("D:显示>=80分以上学生\n");
printf("请选择(A~D):");
return getchar();
}
int main() {
for (i = 0; i < 5; i++) {
printf("请输入学号:\n");
scanf("%d", &num[i]);
printf("请输入成绩:\n");
scanf("%f", &score[i]);
sum = sum + score[i];
}
switch (Menu()) {
case 'A':
PrintAll();
break;
case 'B':
ComputeAverage();
break;
case 'C':
Sort();
break;
case 'D':
PrintCxcellent;
break;
}
return 0;
}
//你看我改动的,估计老师出题应该我这样理解的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询