C#我怎么能算上C#程序员的数量 [英] C# how can I count amount of C# programmers

查看:66
本文介绍了C#我怎么能算上C#程序员的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过函数计算C#程序员的数量。例如我有开发人员数组`



开发人员[] d =新开发人员[5]

{

新的FrontEndDeveloper(Hakob,Hakobyan,23,2.5,C#),

新的BackEndDeveloper(Petros, Petrosyan,25,5,Python),

新的FullStackDeveloper(Poghos,Poghosyan,30,8,C#),

新的BackEndDeveloper (Valod,Aramyan,24,3.5,JavaScript),

新的FrontEndDeveloper(Sanasar,Araqelyan,26,4,C#),

};



我必须在Developer Class中使用函数,其名称为AmountofC#Programmers();

当我愿意在主方法中调用此函数,它将打印有3个C#程序员



我尝试过:



我试过这样的话`



班级开发人员



public void AmountofC#Programmers()

{

int amount = 0;

if(languages ==C# )

{

金额++;

Console.WriteLine(有{0} C#程序员,金额);

}



}



static void Main()



开发人员[] d =新开发人员[5]

{

新FrontEndDeveloper(Hakob,Hakobyan,23,2.5 ,C#),

新的BackEndDeveloper(Petros,Petrosyan,25,5,Python),

新的FullStackDeveloper(Poghos, Poghosyan,30,8,C#),

新的BackEndDeveloper(Valod,Aramyan,24,3.5,JavaScript),

新的FrontEndDeveloper (Sanasar,Araqelyan,26,4,C#),

};



foreach(开发者项目在d)

{

item.AmountofC#Programmers();

}



但它没有用。

It's need me count amount of C# programmers through function.For example i have Developer arrays`

Developer[] d = new Developer[5]
{
new FrontEndDeveloper("Hakob", "Hakobyan", 23, 2.5, "C#"),
new BackEndDeveloper("Petros", "Petrosyan", 25, 5, "Python"),
new FullStackDeveloper("Poghos", "Poghosyan", 30, 8, "C#"),
new BackEndDeveloper("Valod", "Aramyan", 24, 3.5, "JavaScript"),
new FrontEndDeveloper("Sanasar", "Araqelyan", 26, 4, "C#"),
};

I must have function in Developer Class, which name is AmountofC#Programmers();
When i will call this function in Main Method, it will print "There are 3 C# Programmers"

What I have tried:

I tried like this`

Class Developer

public void AmountofC#Programmers()
{
int amount = 0;
if(languages == "C#")
{
amount++;
Console.WriteLine("There are {0} C# Programmers",amount);
}

}

static void Main()

Developer[] d = new Developer[5]
{
new FrontEndDeveloper("Hakob", "Hakobyan", 23, 2.5, "C#"),
new BackEndDeveloper("Petros", "Petrosyan", 25, 5, "Python"),
new FullStackDeveloper("Poghos", "Poghosyan", 30, 8, "C#"),
new BackEndDeveloper("Valod", "Aramyan", 24, 3.5, "JavaScript"),
new FrontEndDeveloper("Sanasar", "Araqelyan", 26, 4, "C#"),
};

foreach (Developer item in d)
{
item.AmountofC#Programmers();
}

But it's not working.

推荐答案

你可以跟踪一个计数器变量,当你遇到C#程序员同时迭代数组中的所有项时,你会增加。这将是你的循环:

You can keep track of a counter variable, which you increase when you come across a C# programmer while iterating over all items in the array. This would be your loop:
int counter = 0;

foreach (Developer item in d)
{
    if (item.Language == "C#") // change 'Language' into the correct identifier
    {
        counter++;
    }
}

Console.WriteLine("There are {0} C# programmers", counter);



你尝试这样做的方式是行不通的:除了你不能把'# '在方法名称中,Developer类本身不是放置AmountOfCSharpProgrammers方法的正确位置,因为Developer类只有一个开发人员的信息,而对于此计数器功能,您需要以下信息:各种各样的开发人员。


The way you try to do it wouldn't work: aside from the fact that you cannot put '#' in method names, the Developer class itself isn't the right place to put an AmountOfCSharpProgrammers method, because the Developer class only has information about one developer, whereas for this counter functionality, you need the information of the full array of developers.


Ahem ...





Console.WriteLine(
Ahem...


Console.WriteLine(


有{d.Count(x => x.Language ==C#)} C#程序员);
"There are {d.Count(x=>x.Language=="C#")} C# programmers");


这篇关于C#我怎么能算上C#程序员的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆