Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c、 将自定义数组限制为仅显示最高值_C#_Arrays_Limit - Fatal编程技术网

C# c、 将自定义数组限制为仅显示最高值

C# c、 将自定义数组限制为仅显示最高值,c#,arrays,limit,C#,Arrays,Limit,我正在尝试编写一个程序,它接受五个人的姓名和身高,并显示身高值最高的人的姓名和身高。如果有几个人共享最高值,则应显示所有人 目前,我可以打印出自定义数组的值,但我不知道如何限制打印输出仅显示最高值 /马蒂亚斯 public ButtonForm() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } /*

我正在尝试编写一个程序,它接受五个人的姓名和身高,并显示身高值最高的人的姓名和身高。如果有几个人共享最高值,则应显示所有人

目前,我可以打印出自定义数组的值,但我不知道如何限制打印输出仅显示最高值

/马蒂亚斯

    public ButtonForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    /* *************************************************************************** */

    class Person
    {
        public string Name { get; set; }
        public int Height { get; set; }
    }

    private void Button_Generate_Click(object sender, EventArgs e)
    {

        Person[] persons = { new Person { Name=TextBox_Name1.Text, Height=int.Parse(TextBox_Height1.Text)},
                             new Person { Name=TextBox_Name2.Text, Height=int.Parse(TextBox_Height2.Text) },
                             new Person { Name=TextBox_Name3.Text, Height=int.Parse(TextBox_Height3.Text) },
                             new Person { Name=TextBox_Name4.Text, Height=int.Parse(TextBox_Height4.Text) },
                             new Person { Name=TextBox_Name5.Text, Height=int.Parse(TextBox_Height5.Text) } };

        IEnumerable<Person> query = persons.OrderBy(person => person.Height);

        foreach (Person person in query)
        {
            Console.WriteLine("{0} - {1}", person.Name, person.Height);
        }

    }

    private void groupBox1_Enter(object sender, EventArgs e)
    {

    }

}
}在您的代码中:

    foreach (Person person in query) {
        if(person.Height == query.First().Height) {
             Console.WriteLine("{0} - {1}", person.Name, person.Height);
        }
    }

要获得最高级别的人员,只需使用:

int highest = persons.Max(p => p.Height);
var query = persons.Where(p => p.Height == highest);

这应该能奏效

int maxHeight = persons.Max(p => p.Height);

var tallestPersons = persons.Where(p => p.Height == maxHeight);