【发布时间】:2023-01-12 04:39:31
【问题描述】:
好的,所以我不知道我是否遗漏了什么?基本上,我使用一组海绵宝宝制作了一个小程序,我制作了一个名为 Person 的类。然后尝试使用 foreach 循环并添加年龄。但由于某种原因,它不会显示它们。这是我的代码:
List<Person> people = new List<Person>();
people.Add(new Person(80, "Eugene", "Krabs", "11/30/1942"));
people.Add(new Person(59, "Sheldon", "Plankton", "11/30/1942"));
people.Add(new Person(30, "Spongebob", "Squarepants", "06/17/1986"));
people.Add(new Person(43, "Squidward", "Tentacles", "10/9/1977"));
people.Add(new Person(38, "Patrick", "Star", "06/19/1984"));
people.Add(new Person(37, "Sandy", "Cheeks", "11/19/1977"));
foreach(Person p in people)
{
Console.WriteLine(p.DisplayPerson());
}
Console.WriteLine("\n Adding Brandon Isaac to List __________________________________________________________________________________________");
people.Insert (0,new Person(24, "Brandon", "Isaac", "03/24/1998"));
foreach (Person pe in people)
{
Console.WriteLine(pe.DisplayPerson());
}
int tot = 0;
foreach(Person per in people)
{
tot += per.Age;
}
Console.WriteLine("\ntotal of ages in list: ", tot);
一切运行良好,除了在设置断点后总数为 311 但它不会显示实际值。它也没有给我任何错误
【问题讨论】:
标签: c#