慕粉3445551
2016-06-04 16:32
如下代码最终打印,会吧“平均分是XX,高于平均分的有”也连续打几次,如何修改只把这句话打印1次?
string[] name = new string[4];
int[] score = new int[4];
int sum = 0, avg;
for (int i = 0;i<name.Length;i++)
{
//Console.WriteLine("第{0}位同学的姓名:{0},他的分数:{1}",name[i], int.Parse(score[i]));
//name[i]= Console.ReadLine();
//score[i] = int.Parse(Console.ReadLine());
Console.Write("第{0}位同学的姓名:{1}",i+1,name[i]);
name[i]=Console.ReadLine();
Console.Write("第{0}位同学的分数:{1}",i+1,score[i]);
score[i] = int.Parse(Console.ReadLine());
//求取总分数值
sum += score[i];
}
//求avg,找出高于avg的所有人姓名及其分数
avg = sum / score.Length;
for (int i = 0; i < score.Length; i++)
{
if (score[i] > avg)
{
//Console.WriteLine("平均分是:{0},高于平均分的同学有:{1}",avg, name[i]);
Console.WriteLine("平均分是:" + avg + "高于平均分的同学有:" + name[i]);
}
}
打印平均分不要放在for循环里,只把打印人名放在for里
string[] name = new string[4];
int[] score = new int[4];
int sum = 0, avg;
for (int i = 0; i < name.Length; i++)
{
//Console.WriteLine("第{0}位同学的姓名:{0},他的分数:{1}",name[i], int.Parse(score[i]));
//name[i]= Console.ReadLine();
//score[i] = int.Parse(Console.ReadLine());
Console.Write("第{0}位同学的姓名:{1}", i + 1, name[i]);
name[i] = Console.ReadLine();
Console.Write("第{0}位同学的分数:{1}", i + 1, score[i]);
score[i] = int.Parse(Console.ReadLine());
//求取总分数值
sum += score[i];
}
//求avg,找出高于avg的所有人姓名及其分数
avg = sum / score.Length;
Console.WriteLine("平均分是:" + avg + "高于平均分的同学有:");
for (int i = 0; i < score.Length; i++)
{
if (score[i] > avg)
{
//Console.WriteLine("平均分是:{0},高于平均分的同学有:{1}",avg, name[i]);
Console.WriteLine(name[i]);
}
}
C#开发轻松入门
254118 学习 · 1459 问题
相似问题