列表不按要求打印c#

时间:2016-03-30 09:52:35

标签: c# list loops

因此,当我使用for循环打印我的列表时,它只是无法正确打印。它说这个人的年龄都是59岁,并没有出现在其他年龄段。我程序中的其余代码是正确的,并使用其他代码进行测试,但我认为这里有问题,也许我错误地使用了列表?

 int teenagersCountPerc = (Int32.Parse(GetUserInput("Teenagers percentage?"))) / 100;

        int adultsCountPerc = (Int32.Parse(GetUserInput("Adults percentage?"))) / 100;

        int elderlysCountPerc = (Int32.Parse(GetUserInput("Elderlys percentage?"))) / 100;

        int teenagersCountNum = (int)(teenagersCountPerc * totalPeople);
        int adultsCountNum = (int)(adultsCountPerc * totalPeople);

int temp1 = teenagersCountNum + adultsCountNum;
for (int i = 0; i < teenagersCountNum; i++) //teens
{
    people.Insert(i, new Person() { FirstName = ("firstName" + i) , LastName = ("lastName" + i), Age = Int32.Parse("16") });  //tried with just integer without parse
}
for (int i = teenagersCountNum; i < temp1; i++) //adults
{
        people.Insert(i, new Person() { FirstName = ("firstName" + i), LastName = ("lastName" + i), Age = Int32.Parse("21") });
}
for (int i = temp1; i < people.Capacity; i++) //elderlys
{
    people.Insert(i, new Person() { FirstName = ("firstName" + i), LastName = ("lastName" + i), Age = Int32.Parse("59") });
}

peopleList.Sort((x, y) => x.Age.CompareTo(y.Age));
            for (int i = 0; i < peopleList.Capacity; i++)
            {
                Console.WriteLine("Full Name: " + peopleList.ElementAt(i).FullName);
                Console.WriteLine("Age: " + peopleList.ElementAt(i).Age);
                Console.WriteLine("Paid: " + peopleList.ElementAt(i).Paid);

            }

peopleList是人员列表...只是使用参数的不同方法。 :)这些值应该是temp1中的变量。

1 个答案:

答案 0 :(得分:7)

您应该(学习)使用调试器来逐步执行代码。您很可能会发现您没有为前两个列表生成元素。考虑你的前几行:

int teenagersCountPerc = (Int32.Parse(GetUserInput("Teenagers percentage?"))) / 100;
int adultsCountPerc = (Int32.Parse(GetUserInput("Adults percentage?"))) / 100;
int elderlysCountPerc = (Int32.Parse(GetUserInput("Elderlys percentage?"))) / 100;

假设我在这里输入值10,50和40(为什么我需要输入老年人百分比,不应该是100减去其他两个?你的意思是要求总人数和两个人百分比?)。然后你正在执行整数除法:

int teenagersCountPerc = 10 / 100 = 0; // Note: integer division
int adultsCountPerc = 50 / 100 = 0;    // Note: integer division
int elderlysCountPerc = 40 / 100 = 0;  // Note: integer division

现在接下来的两行

int teenagersCountNum = (int)(teenagersCountPerc * totalPeople);
int adultsCountNum = (int)(adultsCountPerc * totalPeople);

会给teenagersCountNum = adultsCountNum = 0,这意味着你用老年人填写整个列表(persons.Capacity)。

通过将百分比更改为浮点数来修复它,例如

double teenagersCountPerc = (Int32.Parse(GetUserInput("Teenagers percentage?"))) / 100.0;

(请注意,我写了100.0以防止使用整数执行除法并在之后转换为加倍),或者甚至允许您的用户输入一个十进制数字(他们可能想要30.3%的青少年) ):

double teenagersCountPerc = (Double.Parse(GetUserInput("Teenagers percentage?"))) / 100;

(其中.0不是严格需要的,因为除法的第一部分已经使它成为浮点计算。

侧面评论

使用列表的常用方法是:

var persons = new List<Person>();

或者,如果您已经知道要创建的人数:

var persons = new List<Person>(personCount);

然后只需添加到列表的末尾:

persons.Add(new Person() { ... });

这不要求您保留插入下一个人的索引。 您几乎不需要Insert件物品,除非您需要物品进入某个特定位置。在这里它特别没有意义,因为无论如何你都要对列表进行排序。

另请注意,Capacity可能比您想要的更大,您可能希望将列表填充到某个personCount变量,填写后使用Count代替{{1}检查人数。

最后,我不知道Capacity做了什么,但我建议将GetUserInput置于其中并验证输入。现在,我假设Int32.Parse返回GetUserInput - 如果我输入string,那么abcd将抛出异常,程序将崩溃。相反,请考虑类似

的内容
Parse

并处理调用者中结果为int? GetIntegerFromUser() { string input = System.Console.ReadLine(); int result; return int.TryParse(input, out result) ? result : default(int?); } 的情况。