C#的问题求助,小白不懂

为什么会出现如图的报错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();
            }
        }
    }


}

c#

1个回答

    interface IComparer
    {
        int Compare(object a, object b);
    }
这个删除
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

enum ShapeColor { 红色, 绿色, 蓝色, 青色, 品红, 黄色, 黑色 }
interface IDrawable
{
     void Draw();
}

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();
    }
}

用红色画椭圆:圆心在(6.12592910236024, 9.74948341015237),长短半轴为(2.61966875876285, 0.356117491776178)
用品红画椭圆:圆心在(8.12488428695355, 1.72670178195774),长短半轴为(4.4058599809212, 4.40212688613782)
用绿色画圆形:圆心在(9.58758796080369, 4.7557863848078),半径为4.64485788468498
用黑色画圆形:圆心在(9.71539222622076, 8.69980688611968),半径为5.14293387306059
用品红画椭圆:圆心在(2.94806713375639, 0.448584347240899),长短半轴为(5.54093919021121, 3.213630273572)
用蓝色画椭圆:圆心在(9.5345923814618, 9.00718993461094),长短半轴为(6.93360322012268, 0.722116395236047)
用黄色画圆形:圆心在(9.05100854069507, 5.87829523993577),半径为7.28133565153989
用蓝色画椭圆:圆心在(7.72991999878079, 9.75711855560407),长短半轴为(7.72606820693522, 6.50920364377517)
用绿色画椭圆:圆心在(1.49293672362945, 8.03865692021263),长短半轴为(7.97429923805143, 1.66086334346834)
用绿色画椭圆:圆心在(5.69395975009257, 3.2331349063819),长短半轴为(9.24725451471622, 2.10577183501132)

https://www.ideone.com/DQU51i

caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复Z_ckiy: 那你得自己实现sort采纳本回答,重新开一个问写给你。
11 个月之前 回复
Z_ckiy
zhwzhw1 能说一下为什么吗 谢谢。 不过作业中要求要定义这个接口的,该怎么改呢
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问