Почему методы не делают то что должны? - C#
Формулировка задачи:
Я не понимаю, почему мои методы не будут делать то, что я написал им, чтобы я имел в виду, что хочу добавить «Добавить животное», «Удалить животное» и распечатать всех животных в зоопарке, я не могу это сделать ПОЧЕМУ?
Листинг программы
- 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:
- //.Add();
- break;
- case 2:
- //.Remove();
- break;
- case 3:
- //.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);
- //}
- }
Листинг программы
- class Program
- {
- delegate void GetMessage();
- static void Main(string[] args)
- {
- ZooManagement x = new ZooManagement(10);
- Console.WriteLine("\n");
- Animal petru = new Fish("Piranha", 22, 33, "M", "round");
- Animal petra = new Reptile("Snake", 33, 44, "F", "long");
- x.Add(4, petru);
- x.Add(5, petra);
- x.Display();
- x.Swap(petru, petra);
- x.Display();
- Console.ReadKey();
- x.DisplayMenu();}}
Листинг программы
- class Fish : Animal, IFood ,ILimbless
- {
- public string shape;
- 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;
- }
- }
Листинг программы
- public abstract class Animal
- {
- public string raceInfo;
- public int weight;
- public double size;
- public string sex;
- public string RaceInfo { get; set; }
- public int Weight { get; set; }
- public double Size { get; set; }
- public string Sex { get; set; }
- public Animal(string raceInfo, int weight, double size, string sex)
- {
- this.raceInfo = raceInfo;
- this.weight = weight;
- this.size = size;
- this.sex = sex;
- }
- public abstract string Behavior();
- public abstract string MakeNoise();
- }
- }
Решение задачи: «Почему методы не делают то что должны?»
textual
Листинг программы
- public abstract class Animal
- {
- public string raceInfo;
- public string RaceInfo { get; set; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д