为什么会出现如图的报错sos
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace wcnm
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Circle
{
enum ShapeColor { 红色, 绿色, 蓝色, 青色, 品红, 黄色, 黑色 }
interface IDrawable
{
void Draw();
}
interface IComparer
{
int Compare(object a, object b);
}
public class ShapeComparer : IComparer
{
int Type;
public ShapeComparer(int Type)
{
this.Type = Type;
}
public int Compare(object a, object b)
{
if(Type==0) //根据x排序
{
if (((Shape)a).CenterX > ((Shape)b).CenterX)
return 1;
else if (((Shape)a).CenterX < ((Shape)b).CenterX)
{
return -1;
}
else
return 0;
}
else if(Type==1) //根据y排序
{
if (((Shape)a).CenterY > ((Shape)b).CenterY)
return 1;
else if (((Shape)a).CenterY < ((Shape)b).CenterY)
{
return -1;
}
else
return 0;
}
else if(Type==2) //根据面积
{
if (((Shape)a).Area > ((Shape)b).Area)
return 1;
else if (((Shape)a).Area < ((Shape)b).Area)
{
return -1;
}
else
return 0;
}
else //根据周长
{
if (((Shape)a).Perimeter > ((Shape)b).Perimeter)
return 1;
else if (((Shape)a).Perimeter < ((Shape)b).Perimeter)
{
return -1;
}
else
return 0;
}
}
}
abstract class Shape:IDrawable, IComparable
{
public double CenterX, CenterY, RadiusLong, RadiusShort;
public int CompareTo(object o)
{
if (RadiusLong > ((Shape)o).RadiusLong)
return 1;
else if (RadiusLong == ((Shape)o).RadiusLong)
return 0;
else
return -1;
}
public int Type;
public void Draw()
{
if (Type == 0)
System.Console.WriteLine($"用{LineColor}画圆形:圆心在({CenterX}, {CenterY}),半径为{RadiusLong}");
else
System.Console.WriteLine($"用{LineColor}画椭圆:圆心在({CenterX}, {CenterY}),长短半轴为({RadiusLong}, {RadiusShort})");
}
public Shape(ShapeColor color, double CenterX, double CenterY, double RadiusLong, double RadiusShort, int Type)
{
LineColor = color;
this.CenterX = CenterX;
this.CenterY = CenterY;
this.Type = Type;
if (Type == 0)
this.RadiusLong = this.RadiusShort = RadiusShort;
else
this.RadiusLong = Math.Max(RadiusShort, RadiusLong); this.RadiusShort = Math.Min(RadiusLong, RadiusShort);
}
public double Area
{
get { return Math.PI * RadiusLong * RadiusShort; }
}
public double Perimeter
{
get { return 2 * Math.PI * RadiusShort + 4 * (RadiusLong - RadiusShort); }
}
public ShapeColor LineColor { get; set; }
}
class Ellipse :Shape
{
public Ellipse(ShapeColor color, double x, double y, double RL, double RS, int t) : base(color, x, y, RL, RS, t) { }
}
class Program
{
static void Main(string[] args)
{
Random rd = new Random();
Shape[] arrs = new Shape[10];
for(int i=0;i<10;i++)
{
arrs[i] = new Ellipse((ShapeColor)rd.Next(0, 7), rd.NextDouble() * 10, rd.NextDouble() * 10, rd.NextDouble() * 10, rd.NextDouble() * 10, rd.Next(0, 2));
}
Array.Sort(arrs);
for(int i=0;i<10;i++)
{
arrs[i].Draw();
}
Array.Sort(arrs, new ShapeComparer(1));//按Y坐标进行排序
System.Console.ReadKey();
}
}
}
}