static void Main(string[] args) { var shapes = new List <Shape>(); foreach (ShapeType shapeType in Enum.GetValues(typeof(ShapeType))) { shapes.Add(ShapeFactory.GetShape(shapeType)); } foreach (var shape in shapes) { shape.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Shape shape; ShapeFactory shapeFactory = new ShapeFactory(); string[] shapes = { "RECTANGLE", "SQUARE", "TRIANGLE" }; Random rand = new Random(); double totalArea = 0; //有效的总面积 double tempArea; for (int i = 0; i < 10; i++) { shape = shapeFactory.GetShape(shapes[rand.Next(3)]); tempArea = shape.AreaCalculate(); if (tempArea != -1) { totalArea += tempArea; } Console.WriteLine(shape.ToString()); } Console.WriteLine("The total area of the legal shapes above are " + totalArea); }
private static void Main() { double totalArea = 0; for (var i = 1; i <= 10; i++) { var randomShapeNum = new Random().Next(0, 3); var randomShape = randomShapeNum switch { 0 => "Rectangle", 1 => "Square", 2 => "Triangle", _ => null }; double[] sides = { new Random().NextDouble() * 10, new Random().NextDouble() * 10, new Random().NextDouble() * 10 }; var shape = ShapeFactory.GetShape(randomShape, sides); if (shape != null && shape.IsValid()) { totalArea += shape.GetArea(); } } Console.WriteLine($"Total area is {totalArea}"); } }