数组只打印一个数字

时间:2015-10-27 20:38:20

标签: asp.net arrays

我在asp:Label中打印数组时遇到问题。数组是5个数字长,但只有当我执行id.Text = arrayname时才打印出其中一个数字;

以下是方法:

void random4helper()
{
    Random rand = new Random();

    int min = 1;
    int max = 51;
    int randomNum;
    int i = 0;
    int count = 0;
    bool loop = true;

    while (loop)
    {
        randomNum = rand.Next(min, max);

        if (!meganumbers4.Contains(randomNum))
        {
            meganumbers4[i] = randomNum;
            count += 1;
            i += 1;
        }

        if (count == 5)
        {
            loop = false;
        }
    }


    for (int j = 0; j < meganumbers4.Length; j++)
    {
        d.Text = meganumbers4[j] + " ";
    }
}

以下是相应的标签:

<asp:Label runat="server" id="a" CssClass="print" Text="A"></asp:Label>

1 个答案:

答案 0 :(得分:2)

您正在覆盖每个循环中的值。相反,您需要将新值添加到当前值。改变这一行:

d.Text = meganumbers4[j] + " ";

要:

d.Text += meganumbers4[j] + " ";