如何在对象列表(C#)中获取函数的最大值-初学者-

如何在对象列表(C#)中获取函数的最大值-初学者-,c#,list,C#,List,第一篇文章在这里,我希望这是我应该问问题的方式, 因此,我在不同的类中使用了一个函数(覆盖),我需要在不同的数据中显示这个函数的最大值。(不知道我说的是否正确) 首先尝试了这个,但它一直给我相同的答案,即使它不是最大值: class Program { static void Main(string[] args) { Trapezoid[] Shape = new Trapezoid[5]; Shape[

第一篇文章在这里,我希望这是我应该问问题的方式, 因此,我在不同的类中使用了一个函数(覆盖),我需要在不同的数据中显示这个函数的最大值。(不知道我说的是否正确)

首先尝试了这个,但它一直给我相同的答案,即使它不是最大值:

class Program
    {
        static void Main(string[] args)
        {
            Trapezoid[] Shape = new Trapezoid[5];

            Shape[0] = new Trapezoid(4,3.5,2);
            Shape[1] = new Rectangle(9,2);
            Shape[2] = new Square(5);
            Shape[3] = new Trapezoid(2,6.8,10);
            Shape[4] = new Square(8);
            
            Trapezoid maximumArea = null;
            
            foreach (Trapezoid trapezoid1 in Shape)
            {
                double area1 = trapezoid1.Area();
                
                foreach (Trapezoid trapezoid2 in Shape)
                {
                    double area2 = trapezoid1.Area();

                    if (area1 >= area2)
                    {
                        maximumArea = trapezoid1;
                    }
                    else
                    {
                        maximumArea = trapezoid2;
                    }
                }
            Console.WriteLine(maximumArea.ToString());
            Console.ReadLine();
        }
    }
第二次我尝试了这个方法,但没有成功:

    class Program
    {
        static void Main(string[] args)
        {
            Trapezoid[] Shape = new Trapezoid[5];

            Shape[0] = new Trapezoid(4,3.5,2);
            Shape[1] = new Rectangle(9,2);
            Shape[2] = new Square(5);
            Shape[3] = new Trapezoid(2,6.8,10);
            Shape[4] = new Square(8);
            
            Trapezoid maximumArea = null;

            foreach (Trapezoid trapezoid1 in Shape)
            {
                double area1 = trapezoid1.Area();

                foreach (Trapezoid trapezoid2 in Shape)
                {
                    double area2 = trapezoid2.Area();

                    maximumArea = Math.Max (area1 , area2);
                }
            }

            Console.WriteLine(maximumArea.ToString());
            Console.ReadLine();

        }
    }
如果您需要查看该功能,请单击“区域”:

class Trapezoid
    {
        protected double a,b,h;

        public Trapezoid(double a, double b, double h)
        {
            this.a = a;
            this.b = b;
            this.h = h;
        }

        public virtual double Area()
        {            
            return ((a + b) * h) / 2;
        }
        public override string ToString()
        {
            return "The area of this Trapezoid is " + Area() + ".";
        }
    }
通过重写(继承的类)在矩形类和方形类中执行相同的操作


我希望你能帮助我,谢谢。:)

您需要使用
maximurea
来存储列表的最大值,而不是当前对的最大值

Trapezoid maximumArea = new Square(0);  //Smallest possible trapezoid

foreach (Trapezoid t in Shape)
{
    if (t.Area() > maximumArea.Area()) maximumArea = t;
}
Console.WriteLine(maximumArea);
您还可以使用LINQ大大缩短此时间:

var maximumArea = shape.OrderByDescending( x => x.Area() ).First();
如果有可能一些梯形连接,也许你应该得到一个列表:

var max = shape.Max( x => x.Area() );
var maximumAreas = shape.Where( x => x.Area() == max).ToList();
使用Linq的电源:

using System.Linq;
然后


你说“做了同样的事”是什么意思?您没有显示该代码,因此我们无法确定您是否理解继承。请参阅副本。只需将对
Area()
的调用替换为这些示例中的属性。
var trapezoidWithTheLargestArea = Shape.OrderByDescending(s => s.Area()).First()