我有一个程序,我加载有关狗的信息。然后我需要找到特定品种的最老的狗。问题是,我的程序没有打印任何东西。
以下是代码:
private static int findOldestAge(Dog[] dogs, string breed)
{
int maxAge = 0;
for (int i = 0; i < dogCount; i++)
{
if (dogs[i].Age > maxAge)
maxAge = dogs[i].Age;
}
return maxAge;
}
private static Dog[] findOldestDogs(Dog[] dogs, string breed, int age)
{
Dog[] oldestDogs = new Dog[MaxNumberOfDogs];
for (int i = 0; i < dogCount; i++)
{
if (dogs[i].Age == age)
oldestDogs[oldestDogCount] = dogs[i];
}
return oldestDogs;
}
static void PrintDogsToConsole(Dog[] dogs, int count)
{
for (int i = 0; i < count; i++)
{
Console.WriteLine("Vardas: {0}\nMikroschemos ID: {1}\nSvoris: {2}\nAmžius: {3}\nVeislė: {4}\nSavininkas: {5}\nTelefonas: {6}\nVakcinacijos data: {7}\nAgresyvus: {8}", dogs[i].Name, dogs[i].ChipId, dogs[i].Weight, dogs[i].Age, dogs[i].Breed, dogs[i].Owner, dogs[i].Phone, dogs[i].VaccinationDate, dogs[i].Aggressive);
}
}
这是主要方法。我非常确定数据是否正常加载,因为如果我愿意,它会打印所有信息。
Dog[] dogs = new Dog[MaxNumberOfDogs];
Console.WriteLine("Which breed dogs count?");
breed = Console.ReadLine();
Dog[] oldestDogs = findOldestDogs(dogs, breed, findOldestAge(dogs, breed));
PrintDogsToConsole(oldestDogs, oldestDogCount);