C# 如何将积垢添加到我的应用程序

C# 如何将积垢添加到我的应用程序,c#,.net,console-application,crud,C#,.net,Console Application,Crud,我是一个初学者,我想在我的控制台应用程序中添加一些CRUD功能。我现在正在努力,因为我想在数组中添加一个fish类型的对象和一个爬行动物类型的对象,然后打印它们,我想通过使用我创建的菜单来实现这一点,但我不知道怎么做。我从代码中添加对象所做的基本测试向我抛出了一个错误,即它超出了界限。我现在有点困惑,我很确定我的问题没有得到很好的解释,但我希望有人理解我需要帮助的是我的课程 这是一个类,它具有添加动物、删除和显示的功能,这个类还包括菜单 public class ZooManagement {

我是一个初学者,我想在我的控制台应用程序中添加一些CRUD功能。我现在正在努力,因为我想在数组中添加一个fish类型的对象和一个爬行动物类型的对象,然后打印它们,我想通过使用我创建的菜单来实现这一点,但我不知道怎么做。我从代码中添加对象所做的基本测试向我抛出了一个错误,即它超出了界限。我现在有点困惑,我很确定我的问题没有得到很好的解释,但我希望有人理解我需要帮助的是我的课程

这是一个类,它具有添加动物、删除和显示的功能,这个类还包括菜单

public class ZooManagement
{
    public Animal[] AnimalsList;
    public int length = 0;
    public int max_size;

    public void DisplayMenu() {
        int choice;
        try
        {
            do
            {
                Console.Write("Welcome to our zoo menu\n\n");
                Console.Write("1. To add Animal\t");
                Console.Write("2. To remove Animal\t");
                Console.Write("3. To Display Animals ");

                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        x.Add();
                        break;
                    case 2:
                        x.Remove();
                        break;
                    case 3:
                        x.Display();
                        break;
                    default:
                        Console.WriteLine("You've pressed something elss");
                        break;
                }
                Console.Write("\n\n\t\t\tNow press any button");
                Console.ReadLine();
                Console.Clear();
            }
            while (choice != 3);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Dont't be like that");
        }
    }      


    public ZooManagement(int x)
    {
        this.AnimalsList = new Animal[x];
        max_size = x;
    }

    //public void Array(int x)
    //{
    //    this.AnimalsList = new Animal[x];
    //    max_size = x;
    //}

    public void Add(Animal x)
    {
        if (this.length > this.max_size)
        {
            Console.WriteLine("Out of bound Exception, Array full");
        }
        else
        {
            this.AnimalsList[this.length] = x;
            this.length++;
        }
    }

    public void Add(int h, Animal x)
    {
        if (h > this.length || h < 0)
        {
            Console.WriteLine("Out of bound Exception");
        }
        else
        {
            for (int i = (this.length); i >= h; i--) {
                this.AnimalsList[i + 1] = this.AnimalsList[i];
            }
            this.AnimalsList[h] = x;
            this.length++;
        }
    }

    public void Remove(Animal x)
    {
        int FoundAt = -1;
        for (int i = 0; i < this.length; i++)
        {
            if (this.AnimalsList[i] == x)
            {
                FoundAt = i;
                break;
            }
        }
        if (FoundAt != -1)
        {
            for (int i = FoundAt; i < this.length; i++)
            {
                this.AnimalsList[i] = this.AnimalsList[i + 1];
            }
            this.length--;
        }
    }

    public void Remove(int x)
    {
        if (x > this.length || x < 0)
        {
            Console.WriteLine("Out of bounds exception");
        }
        else
        {
            this.AnimalsList[x] = null;
            for (int i = x; i < this.length; i++)
            {
                this.AnimalsList[i] = this.AnimalsList[i + 1];
            }
            this.length--;
        }
    }


    public Animal GetAnimal(string reign)
    {
        if ("fish".Equals(reign))
        {
            return new Fish("", 0, 0, "", "");
        }
        else if ("reptile".Equals(reign))
        {
            return new Reptile("", 0, 0, "", "");
        }
        return null;
    }

    public Animal Get(int x)
    {
        if (x > this.length || x < 0)
        {
            Console.WriteLine("Out of bounds Exeption");
            return null;
        }
        else
        {
            return this.AnimalsList[x];
        }
    }
    public void Set(int h, Animal x)
    {
        if (h > this.length || h < 0)
        {
            Console.WriteLine("Out of bounds Exeption");
        }
        else
        {
            this.AnimalsList[h] = x;
        }
    }

    public void Swap(int x, int y)
    {
        if (x >= 0 && x < length && y >= 0 && y < length)
        {
            Animal temp = AnimalsList[x];
            AnimalsList[x] = AnimalsList[y];
            AnimalsList[y] = temp;
        }
        else
        {
            Console.WriteLine("Out of bounds exception");
        }
    }

    public void Swap(Animal x, Animal y)
    {
        int FoundAtA = -1;
        int FoundAtB = -1;

        for(int i = 0; i <this.length; i++)
        {
            if(this.AnimalsList[i] == x)
            {
                FoundAtA = i;
                if(FoundAtA != -1 && FoundAtB != -1)
                    {
                    break;
                    } 
            }
            else if(this.AnimalsList[i] == y)
            {
                FoundAtB = i;
                if(FoundAtA != -1 && FoundAtB != -1)
                {
                    break;
                }
        }
    }
        if (FoundAtA != -1 && FoundAtB != -1)
        {
            this.Swap(FoundAtA, FoundAtB);
        }
        else
        {
            Console.WriteLine("Out of bounds Exeption");
        }
    }

    public void Display()
    {
        for (int i = 0; i < this.length; i++)
        {
            Console.WriteLine(this.AnimalsList[i]);
        }
    }
}
    //public static double AnimalWeight(int weight, int size)
    //{
    //    return (weight * 703) / (size * size);
    //}
}
这是一个fish类,它拥有抽象动物类的一些属性和行为

class Fish : Animal, IFood ,ILimbless
{

    public string shape;
    private string v;

    public string Shape { get; set; }

    private string GetFishShape(string shape)
    {
        return shape;
    }

    public override string Behavior()
    {
        return "Passive";
    }

    public void  Eats()
    {
        Console.WriteLine( "Eats alges and fish");
    }

    public override string MakeNoise()
    {
        return "Doesnt make noises LOL";
    }


    public void Slither(string w)
    {
        Console.WriteLine("It is limbless that's why it slithers");
    }

    public Fish(string raceInfo, int weight, double size, string sex, string shape) : base(raceInfo, weight, size, sex)
    {
        this.raceInfo = raceInfo;
        this.weight = weight;
        this.size = size;
        this.sex = sex;
        this.shape = shape;
    }

}

}

您从未清洁过
长度
。为了确保确实正确使用了length,您应该在初始化时再次将length设置为0

public ZooManagement(int x)
{
    this.AnimalsList = new Animal[x];
    this.max_size = x;
    this.length = 0;
}

请考虑重命名变量<代码>长度>代码>,因为在数组中它有不同的含义。


旁注:
最好也删除ifs,检查它是否在范围内。如果它不在界限之内,它将抛出一个比这个更明显的错误。如果您不希望出现错误,请使用trycatch,如果失败,请确保您记录了唯一且清晰的错误。

这看起来像是家庭作业,是吗?一般来说,你不会得到太多的帮助,如果有的话,家庭作业。这些和“积垢”有什么关系?关注你所遇到的具体错误。哪一行抛出错误?发生时的值是什么?@J.Doe那么这是一个开始学习编程的好地方(因为从大三到大四的大多数问题都是通过调试解决的。)你写的
代码向我抛出了一个越界的错误
没有任何问题你应该回答自己为什么它会抛出错误,因为您编写了代码行
Console.WriteLine(“越界异常”)因为这是您的代码,所以您应该知道为什么要通知错误。我们已经到了您要求我们调试代码的地步。这在SO中是离题的。请阅读以下内容:。现在是学习调试的时候了。这将颠覆您的编程体验。:)
public ZooManagement(int x)
{
    this.AnimalsList = new Animal[x];
    this.max_size = x;
    this.length = 0;
}