我是初学者我想在控制台应用中添加一些CRUD功能。我现在正在努力,因为我想在数组中添加一个类型为鱼的Object和一个类型为Reptile的Object然后打印它们,我想通过使用我创建的菜单来实现它但我不能弄清楚如何做到这一点。我从代码中添加对象的基本测试会引发错误,它超出范围。我现在非常困惑,我很确定我的问题解释得很差,但我希望有人能理解我需要的帮助,这里是我的课程
以下是具有添加动物功能的类,或者删除和显示的功能,此功能还包括菜单
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);
//}
}
*这是主要的
public class Program
{
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();
}
}
以下是具有抽象Animl类的一些统计数据和行为的鱼类
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;
}
}
}
答案 0 :(得分:0)
您永远不会清除length
。为了确保确实使用了长度,你应该在初始化时再将长度设置为0。
public ZooManagement(int x)
{
this.AnimalsList = new Animal[x];
this.max_size = x;
this.length = 0;
}
接下来请考虑重命名变量length
,因为在数组中它确实有不同的含义。
旁注: 删除ifs也可能是明智的,检查它是否在边界内。如果它不在界限范围内,那么它会抛出一个更清晰的错误。如果您不希望弹出错误,那么请使用trycatch,如果失败,请确保记录一个唯一且明确的错误。