列表不打印为通缉c#

0 c# loops list

因此,当我使用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);

            }
Run Code Online (Sandbox Code Playgroud)

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

Com*_*hip 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;
Run Code Online (Sandbox Code Playgroud)

假设我在这里输入值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
Run Code Online (Sandbox Code Playgroud)

现在接下来的两行

int teenagersCountNum = (int)(teenagersCountPerc * totalPeople);
int adultsCountNum = (int)(adultsCountPerc * totalPeople);
Run Code Online (Sandbox Code Playgroud)

将给出teenagersCountNum = adultsCountNum = 0persons.Capacity用老年人填写整个列表()的含义.

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

double teenagersCountPerc = (Int32.Parse(GetUserInput("Teenagers percentage?"))) / 100.0;
Run Code Online (Sandbox Code Playgroud)

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

double teenagersCountPerc = (Double.Parse(GetUserInput("Teenagers percentage?"))) / 100;
Run Code Online (Sandbox Code Playgroud)

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


一边说

使用列表的常用方法是:

var persons = new List<Person>();
Run Code Online (Sandbox Code Playgroud)

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

var persons = new List<Person>(personCount);
Run Code Online (Sandbox Code Playgroud)

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

persons.Add(new Person() { ... });
Run Code Online (Sandbox Code Playgroud)

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

另请注意,Capacity可能比您想要的更大,您可能希望将列表填充到某个personCount变量,并在填写后使用Count而不是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?);
}
Run Code Online (Sandbox Code Playgroud)

并处理结果null在您的调用者中的情况.