static void Main(string[] args) { var shapeList = new List <Shape>(); float triangleSum = 0, totalArea = 0; Shape3D my3DShape = null; for (int i = 0; i < 20; i++) { shapeList.Add(Shape.GenerateShape(new Vector3(3.0f, 3.0f, 4.0f))); } foreach (var shape in shapeList) { if (shape is Shape3D) { if (my3DShape == null) { my3DShape = (Shape3D)shape; } var maxVol = (Shape3D)shape; if (maxVol.Volume > my3DShape.Volume) { my3DShape = maxVol; } } if (shape is Triangle) { var triangle = (Triangle)shape; triangleSum += triangle.Circumference; } Console.WriteLine($"{shape}"); totalArea += shape.Area; } Console.WriteLine($"\nAverage area of all shapes: {totalArea / 20:0.0}"); Console.WriteLine($"Circumference of all triangles: {triangleSum:0.0}"); Console.WriteLine($"The 3D shape with the largest volume is: {my3DShape}\nWith a volume of: {my3DShape.Volume:0.0}"); }
static void Main(string[] args) { int noOfShapes = 20; float totArea = 0.0f; float totCircumference = 0.0f; Shape3D biggest3D = null; List <Shape> shapes = new List <Shape>(); for (int i = 0; i < noOfShapes; i++) { shapes.Add(Shape.GenerateShape()); } foreach (Shape shape in shapes) { Console.WriteLine(shape.ToString()); totArea += shape.Area; if (shape is Triangle) { totCircumference += (shape as Shape2D).Circumference; } if (shape is Shape3D && (biggest3D == null || (shape as Shape3D).Volume > biggest3D.Volume)) { biggest3D = shape as Shape3D; } } Console.WriteLine(""); Console.WriteLine($"Total omkrets för alla trianglar: {totCircumference:0.0}."); Console.WriteLine(""); Console.WriteLine($"Genomsnittlig Area: {(totArea / noOfShapes):0.0}."); Console.WriteLine(""); Console.WriteLine(value: $"Största 3D shape (volymmässigt) är: {biggest3D?.ToString()} som har volymen {biggest3D.Volume:0.0}."); }