Почему методы не делают то что должны? - C#

Узнай цену своей работы

Формулировка задачи:

Я не понимаю, почему мои методы не будут делать то, что я написал им, чтобы я имел в виду, что хочу добавить «Добавить животное», «Удалить животное» и распечатать всех животных в зоопарке, я не могу это сделать ПОЧЕМУ?
Листинг программы
  1. public class ZooManagement
  2. {
  3. public Animal[] AnimalsList;
  4. public int length = 0;
  5. public int max_size;
  6. public void DisplayMenu() {
  7. int choice;
  8. try
  9. {
  10. do
  11. {
  12. Console.Write("Welcome to our zoo menu\n\n");
  13. Console.Write("1. To add Animal\t");
  14. Console.Write("2. To remove Animal\t");
  15. Console.Write("3. To Display Animals ");
  16. choice = int.Parse(Console.ReadLine());
  17. switch (choice)
  18. {
  19. case 1:
  20. //.Add();
  21. break;
  22. case 2:
  23. //.Remove();
  24. break;
  25. case 3:
  26. //.Display();
  27. break;
  28. default:
  29. Console.WriteLine("You've pressed something elss");
  30. break;
  31. }
  32. Console.Write("\n\n\t\t\tNow press any button");
  33. Console.ReadLine();
  34. Console.Clear();
  35. }
  36. while (choice != 3);
  37. }
  38. catch (Exception ex)
  39. {
  40. Console.WriteLine("Dont't be like that");
  41. }
  42. }
  43.  
  44. ////////////////////////////////////////////////////
  45. public ZooManagement(int x)
  46. {
  47. this.AnimalsList = new Animal[x];
  48. max_size = x;
  49.  
  50. }
  51. //public void Array(int x)
  52. //{
  53. // this.AnimalsList = new Animal[x];
  54. // max_size = x;
  55. //}
  56.  
  57. public void Add(Animal x)
  58. {
  59. if (this.length > this.max_size)
  60. {
  61. Console.WriteLine("Out of bound Exception, Array full");
  62. }
  63. else
  64. {
  65. this.AnimalsList[this.length] = x;
  66. this.length++;
  67. }
  68. }
  69. public void Add(int h, Animal x)
  70. {
  71. if (h > this.length || h < 0)
  72. {
  73. Console.WriteLine("Out of bound Exception");
  74. }
  75. else
  76. {
  77. for (int i = (this.length); i >= h; i--) {
  78. this.AnimalsList[i + 1] = this.AnimalsList[i];
  79. }
  80. this.AnimalsList[h] = x;
  81. this.length++;
  82. }
  83. }
  84. public void Remove(Animal x)
  85. {
  86. int FoundAt = -1;
  87. for (int i = 0; i < this.length; i++)
  88. {
  89. if (this.AnimalsList[i] == x)
  90. {
  91. FoundAt = i;
  92. break;
  93. }
  94. }
  95. if (FoundAt != -1)
  96. {
  97. for (int i = FoundAt; i < this.length; i++)
  98. {
  99. this.AnimalsList[i] = this.AnimalsList[i + 1];
  100. }
  101. this.length--;
  102. }
  103. }
  104. public void Remove(int x)
  105. {
  106. if (x > this.length || x < 0)
  107. {
  108. Console.WriteLine("Out of bounds exception");
  109. }
  110. else
  111. {
  112. this.AnimalsList[x] = null;
  113. for (int i = x; i < this.length; i++)
  114. {
  115. this.AnimalsList[i] = this.AnimalsList[i + 1];
  116. }
  117. this.length--;
  118. }
  119. }
  120.  
  121. public Animal GetAnimal(string reign)
  122. {
  123. if ("fish".Equals(reign))
  124. {
  125. return new Fish("", 0, 0, "", "");
  126. }
  127. else if ("reptile".Equals(reign))
  128. {
  129. return new Reptile("", 0, 0, "", "");
  130. }
  131. return null;
  132. }
  133. public Animal Get(int x)
  134. {
  135. if (x > this.length || x < 0)
  136. {
  137. Console.WriteLine("Out of bounds Exeption");
  138. return null;
  139. }
  140. else
  141. {
  142. return this.AnimalsList[x];
  143. }
  144. }
  145. public void Set(int h, Animal x)
  146. {
  147. if (h > this.length || h < 0)
  148. {
  149. Console.WriteLine("Out of bounds Exeption");
  150. }
  151. else
  152. {
  153. this.AnimalsList[h] = x;
  154. }
  155. }
  156. public void Swap(int x, int y)
  157. {
  158. if (x >= 0 && x < length && y >= 0 && y < length)
  159. {
  160. Animal temp = AnimalsList[x];
  161. AnimalsList[x] = AnimalsList[y];
  162. AnimalsList[y] = temp;
  163. }
  164. else
  165. {
  166. Console.WriteLine("Out of bounds exception");
  167. }
  168. }
  169. public void Swap(Animal x, Animal y)
  170. {
  171. int FoundAtA = -1;
  172. int FoundAtB = -1;
  173. for(int i = 0; i <this.length; i++)
  174. {
  175. if(this.AnimalsList[i] == x)
  176. {
  177. FoundAtA = i;
  178. if(FoundAtA != -1 && FoundAtB != -1)
  179. {
  180. break;
  181. }
  182. }
  183. else if(this.AnimalsList[i] == y)
  184. {
  185. FoundAtB = i;
  186. if(FoundAtA != -1 && FoundAtB != -1)
  187. {
  188. break;
  189. }
  190. }
  191. }
  192. if (FoundAtA != -1 && FoundAtB != -1)
  193. {
  194. this.Swap(FoundAtA, FoundAtB);
  195. }
  196. else
  197. {
  198. Console.WriteLine("Out of bounds Exeption");
  199. }
  200. }
  201. public void Display()
  202. {
  203. for (int i = 0; i < this.length; i++)
  204. {
  205. Console.WriteLine(this.AnimalsList[i]);
  206. }
  207. }
  208. }
  209. //public static double AnimalWeight(int weight, int size)
  210. //{
  211. // return (weight * 703) / (size * size);
  212. //}
  213. }
Листинг программы
  1. class Program
  2. {
  3. delegate void GetMessage();
  4. static void Main(string[] args)
  5. {
  6. ZooManagement x = new ZooManagement(10);
  7. Console.WriteLine("\n");
  8. Animal petru = new Fish("Piranha", 22, 33, "M", "round");
  9. Animal petra = new Reptile("Snake", 33, 44, "F", "long");
  10. x.Add(4, petru);
  11. x.Add(5, petra);
  12. x.Display();
  13. x.Swap(petru, petra);
  14. x.Display();
  15. Console.ReadKey();
  16. x.DisplayMenu();}}
Листинг программы
  1. class Fish : Animal, IFood ,ILimbless
  2. {
  3. public string shape;
  4. public string Shape { get; set; }
  5. private string GetFishShape(string shape)
  6. {
  7. return shape;
  8. }
  9. public override string Behavior()
  10. {
  11. return "Passive";
  12. }
  13. public void Eats()
  14. {
  15. Console.WriteLine( "Eats alges and fish");
  16. }
  17. public override string MakeNoise()
  18. {
  19. return "Doesnt make noises LOL";
  20. }
  21.  
  22. public void Slither(string w)
  23. {
  24. Console.WriteLine("It is limbless that's why it slithers");
  25. }
  26. public Fish(string raceInfo, int weight, double size, string sex, string shape) : base(raceInfo, weight, size, sex)
  27. {
  28. this.raceInfo = raceInfo;
  29. this.weight = weight;
  30. this.size = size;
  31. this.sex = sex;
  32. this.shape = shape;
  33. }
  34. }
Листинг программы
  1. public abstract class Animal
  2. {
  3. public string raceInfo;
  4. public int weight;
  5. public double size;
  6. public string sex;
  7. public string RaceInfo { get; set; }
  8. public int Weight { get; set; }
  9. public double Size { get; set; }
  10. public string Sex { get; set; }
  11. public Animal(string raceInfo, int weight, double size, string sex)
  12. {
  13. this.raceInfo = raceInfo;
  14. this.weight = weight;
  15. this.size = size;
  16. this.sex = sex;
  17. }
  18. public abstract string Behavior();
  19. public abstract string MakeNoise();
  20. }
  21. }

Решение задачи: «Почему методы не делают то что должны?»

textual
Листинг программы
  1. public abstract class Animal
  2. {
  3. public string raceInfo;
  4.  
  5. public string RaceInfo { get; set; }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.125 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы