【发布时间】:2014-03-21 02:46:42
【问题描述】:
这是我需要解决的问题:
- 找出 12 个分数中的最高值
- 找出 12 个分数中的最低值
- 计算 12 个分数的总和
- 从总分中减去最高分和最低分
- 用总分除以 10 计算剩余 10 个分数的平均值
- 输出平均值(格式为小数点后 2 位)
这是我到目前为止所做的,除了计算总分并从总分中减去最高和最低,我不确定我应该把代码放在哪里以及我应该使用什么代码:
double[] 分数 = { 8.7, 9.3, 7.9, 6.4, 9.6, 8.0, 8.8, 9.1, 7.7, 9.9, 5.8, 6.9 };
Console.WriteLine("Numbers in the list:" + scores.Length);
for (int index = 0; index < scores.Length; index++)
{
Console.WriteLine(scores[index]);
}
//highest number
double high = scores[0];
for (int index = 1; index < scores.Length; index++)
{
if (scores[index] > high)
{
high = scores[index];
}
}
Console.WriteLine("Highest number =" + high);
//lowest number
double low = scores[0];
for (int index = 1; index < scores.Length; index++)
{
if (scores[index] < low)
{
low = scores[index];
}
}
Console.WriteLine("lowest number =" + low);
//average of the scores
double total = 0;
double average = 0;
for (int index = 0; index < scores.Length; index++)
{
total = total + scores[index];
}
average = (double)total / scores.Length;
Console.WriteLine("Total=" + total);
Console.WriteLine("Average=" + average.ToString("N2"));
Console.ReadKey();
}
【问题讨论】:
-
如你所见,用 Linq 做起来并不难,但在回答之前,我想知道你想如何处理列表中可能有多个项目的事实最大值/最小值。在这种情况下,您是否要删除所有具有最大值或最小值的项目?
-
我已经能够做所有其他事情,但我似乎无法得到总数减去最高和最低数字:这是实际问题:找到 12 个分数中的最高值 找到最低值12 分的值 计算 12 分的总和 从总分中减去最高分和最低分 用总分除以 10 计算剩余 10 分的平均值 输出平均值(格式为小数点后 2 位)
-
如果保证这些值是唯一的,那么这里提出的解决方案将起作用。另一方面,假设 9.9 在列表中出现两次,那么您的问题没有指定如何处理它。我们必须假设它的意思是“只取一个最高的,一个最低的”。
标签: c#