如何使用 While 和 Do While 在标签中显示前 100 个偶数? C#

How to show in a label first 100 even numbers using While and Do While ? c#

实际上我有一个按钮可以使用 For

显示前 100 个偶数
    int a = 100;
        int res = 0;
        int i;
        string npares = "";
        for (i = 0; i <= a; i++)
        {
            if (i % 2 == 0)
            {
                res = res + i;
                if (i < a)
                    npares += i + ",";
                else
                    npares += i;
            }


        }
        LBLstatus.MaximumSize = new Size(200, 0);
        LBLstatus.Text = npares;

但是我需要使用 While 和 Do While 来制作更多的两个按钮,我该怎么做?

编辑>>>>>>

在我这样的时候使用:

int a = 100;
        int i = 0;
        string npares = "";
        int res = 0;

        while (i <= a)
        {
            i++;
            if

                (i % 2 == 0)
            {
                res = res + i;
                if (i < a)
                    npares += i + ",";
                else
                    npares += i;
            }
            LBLstatus.Text = npares;

使用Take

List<int> ints;

List<int> positiveInts = ints.Where(i => i % 2 == 0).Take(100).ToList();

使用Aggregate

List<int> ints;

string positiveInts = ints.Where(i => i % 2 == 0).Take(100).Select(i => i.ToString()).Aggregate((a,b) => b += String.IsNullOrEmpty(b) ? a : "," + a);

(此答案显示了构造之间的关系,但不尝试提供解决方案。)


结构

for (init;cond;post)
{
    body;
}

通常可以重写为/认为等同于

init;
while (cond) {
    body;
    post;
}

另一方面,do-while 没有类似的简单 for 形式,因为它将 cond 的计算延迟到主体执行一次之后,但它可以写成

for (init;;post) {
    body;
    if (!cond) break;
}