zhwzhw1 2019-04-02 22:58 采纳率: 0%
浏览 262

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


}

  • 写回答

1条回答

  • threenewbee 2019-04-03 00:32
    关注
        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

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况