1.从键盘上输入若干个学生的成绩,以-1结束,求所有成绩的平均分。要求用while语句完成。

核心语句提示: while(1){ scanf("%d",&score);if(score==-1) break;sum+=score;n++;}
2024-11-22 16:41:32
推荐回答(2个)
回答1:

#include "stdio.h"
main()
{int score;
float sum=0,ave;
int n=0;
while(1) /*循环条件总为真,用有条件的break语句结束循环*/
{scanf("%d",&score);
if(score==-1)
break;
sum=sum+score;
n++;
}
ave=sum/n;
printf("平均成绩=%f\n",ave);
}

回答2:

int n = 0,sum = 0;
float avg = 0.0;

while(1)

{ scanf("%d",&score);

if(score==-1) break;

sum+=score;

n++;}
avg = sum * 1.0 / n;