Esempio n. 1
0
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();
            double       totalArea    = 0;
            Random       random       = new Random();

            for (int i = 1; i <= 10; i++)
            {
                int num = random.Next(1, 4);
                switch (num)
                {
                case 1:
                {
                    Shape shape = shapeFactory.GetShape("CIRCLE");
                    totalArea += shape.Draw();
                    break;
                }

                case 2:
                {
                    Shape shape = shapeFactory.GetShape("RECTANGLE");
                    totalArea += shape.Draw();
                    break;
                }

                case 3:
                {
                    Shape shape = shapeFactory.GetShape("SQUARE");
                    totalArea += shape.Draw();
                    break;
                }

                case 4:
                {
                    Shape shape = shapeFactory.GetShape("TRIANGLE");
                    totalArea += shape.Draw();
                    break;
                }
                }
            }
            Console.WriteLine("随机生成十个图形的面积的总和为:" + totalArea);
            Console.ReadLine();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Random random    = new Random();
            double sumOfArea = 0;

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(10);
                int   x     = random.Next(0, 3);
                Shape shape = ShapeFactory.Manufacture((ShapeName)x);
                if (!shape.Judge())//形状不合法
                {
                    i--;
                    continue;
                }
                Console.WriteLine($"第{i}个形状为:{(ShapeName)x},面积为:{shape.Area()}");
                sumOfArea += shape.Area();
            }
            Console.WriteLine($"总面积为:{sumOfArea}");
        }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            IShape[] shapes = new IShape[10];
            Random rd = new Random();
            for (var i = 0; i < shapes.Length; i++)
            {
                shapes[i] = ShapeFactory.GetShape(rd.Next(3));
            }

            double totalArea = 0;
            foreach (var shape in shapes)
            {
                if (shape.IsLegal())
                {
                    Console.WriteLine($"该图形合法,面积为{shape.Area}");
                    totalArea += shape.Area;
                }

                Console.WriteLine("该图形不合法");
            }

            Console.WriteLine($"总图形面积为{totalArea}");
        }