static void Main(string[] args) { Triangle t = new Triangle(1, 2, 3); Console.WriteLine(t.CalculateArea()); double sumArea = 0; Random random = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < 10; i++) { int tmp = random.Next(1, 4); switch (tmp) { case 1: Random random1 = new Random(Guid.NewGuid().GetHashCode()); Rectangle rectangle = new Rectangle(random1.Next(1, 100), random1.Next(1, 100)); Console.WriteLine("生成了一个矩形,面积为:" + rectangle.CalculateArea()); sumArea += rectangle.CalculateArea(); break; case 2: Random random2 = new Random(Guid.NewGuid().GetHashCode()); Square square = new Square(random2.Next(1, 100)); Console.WriteLine("生成了一个正方形,面积为:" + square.CalculateArea()); sumArea += square.CalculateArea(); break; case 3: Random random3 = new Random(Guid.NewGuid().GetHashCode()); Triangle triangle = null; while (true) { triangle = new Triangle(random3.Next(1, 100), random3.Next(1, 100), random3.Next(1, 100)); if (triangle.IfLegal()) { break; } } Console.WriteLine("生成了一个三角形,面积为:" + triangle.CalculateArea()); sumArea += triangle.CalculateArea(); break; } } Console.WriteLine("总面积为:" + sumArea); }
static void Main(string[] args) { Shape C1 = new Circle() { Height = 10, Radious = 10 }; C1.PrintParameters(); Console.WriteLine("The Area of circle {0}", C1.CalculateArea()); Console.WriteLine("The Volume of cylinder {0}", C1.CalculateVolume()); Console.WriteLine(); Shape R1 = new Rectangle() { Height = 10, Width = 10, Length = 10 }; R1.PrintParameters(); Console.WriteLine("The Area of Rectangle {0}", R1.CalculateArea()); Console.WriteLine("The Volume of Cube {0}", R1.CalculateVolume()); Console.WriteLine(); Shape T1 = new Triangle() { Edge = 10, Trinaglebase = 10, Trinagleheight = 10 }; T1.PrintParameters(); Console.WriteLine("The Area of Triangle {0}", T1.CalculateArea()); Console.WriteLine("The Volume of Triangle {0}", T1.CalculateVolume()); Console.WriteLine(); Console.ReadKey(); }