如何汇总列表中的所有项目? [英] How can I sum all items in a list?
本文介绍了如何汇总列表中的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习C#,但现在遇到了问题.如您所见,我的列表名称是"notlar".当某人按下"2"时,我想要列表项的平均值.我该怎么办"+ listitem1 + listitem2".之后,我想将其写入int sonuc
Hi,
I''m learning c# but i have a problem now. As you see, i have a list name which is "notlar". When someone press "2" i want average of list items. How can I do " +listitem1+listitem2". After that i want to write it to int sonuc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrtalamaHesabı
{
class Program
{
static void Main(string[] args)
{
int secenek, devam = 1;
List<int> notlar = new List<int>();
int sonuc = 0;
while (devam == 1)
{
Console.WriteLine();
Console.WriteLine("Add number ==>");
notlar.Add(int.Parse(Console.ReadLine()));
Console.WriteLine("Add number->1 ,Take arithmatic average ->2, Exit ->3");
secenek = int.Parse(Console.ReadLine());
if (secenek == 3)
devam = 0;
if (secenek == 2)
{
}
}
}
}
}
推荐答案
添加所有列表项:
To add all list items:
using System.Linq;
//...
sonuc = notlar.Sum();
如果您使用带有合适短语的互联网搜索引擎,则无需提出此问题.
在 c#list<>上进行搜索平均.您将获得许多有用的可能性.
If you had used an internet search engine with a suitable phrase, you would not have needed to ask this question.
Do a Search on c# list<> average. You will get lots of useful possibilities.
这篇关于如何汇总列表中的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文