using System;
using System.Collections.Generic;
using System.Linq;
var amount = new List<double>() { 2.5, 1.5, 3.5, 5.5 };
var sum = amount.Sum();
Console.WriteLine(sum);
var highest3 = amount.OrderByDescending(a => a).Take(3);
var i = 1;
foreach (var d in highest3)
{
Console.WriteLine("{0} is position {1}",d,i);
i++;
}
Console.ReadLine();
編輯:框架2
var amount = new List<double>() { 2.5, 1.5, 3.5, 5.5 };
var sum = 0.0d;
foreach (var d in amount)
{
sum += d;
}
Console.WriteLine(sum);
amount.Sort();
amount.Reverse();
lbl_first.Text = amount[0].ToString();
lbl_second.Text = amount[1].ToString();
lbl_third.Text = amount[2].ToString();
var i = 1;
foreach (var d in amount)
{
if (i > 3)
break;
Console.WriteLine("{0} is position {1}",d,i);
i++;
}
Console.ReadLine();
它的2.0,所以我們不能使用Sum等,因爲它需要Linq&Linq可以在4.0中使用:( –
沒有意識到框架...嘗試編輯 – christiandev
它的工作,但我怎麼能打印標籤中的前3個值plz:/對不起,我是Noooooooooob –