0 / 0 / 0
Регистрация: 05.10.2016
Сообщений: 2
|
|
1
|
Почему методы не делают то что должны?
07.08.2017, 15:59. Показов 629. Ответов 2
Я не понимаю, почему мои методы не будут делать то, что я написал им, чтобы я имел в виду, что хочу добавить «Добавить животное», «Удалить животное» и распечатать всех животных в зоопарке, я не могу это сделать ПОЧЕМУ?
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
| 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);
//}
} |
|
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| 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();}} |
|
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| 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;
}
} |
|
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| 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();
}
} |
|
0
|