通过数组循环一定次数,输出转到文本框

时间:2017-10-10 00:16:43

标签: c#

每次按下我的按钮,我都希望我的循环执行,并且每次在textBox1中出现一周中的一个随机日。例如,buttonclick-Tuesday,buttonclick-Thursday,buttonclick-Monday,buttonclick-Friday。我需要循环执行总共四(4)次。然后关闭表格。

for (int i = 0; i <= 4; i++)
{
  String[] strpleaseloop =    { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
  Random r = new Random();
  int iSelect = r.Next(0, 6);
  textBox1.Text = strpleaseloop[iSelect];
  this.Close();
}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你不应该使用循环。相反,使用计数器。这应该可以正常工作。

在按钮外单击:

for (var i = 0; i < compArr.length; i += 1) {
    compArr[i].code = $('#label_text_'+i).text();
}

在按钮内点击:

int count = 0;

另外,我强烈建议您将if (count == 4) this.Close(); count++; String[] strpleaseloop = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; Random r = new Random(); int iSelect = r.Next(0, 6); textBox1.Text = strpleaseloop[iSelect]; 放在按钮单击之外,因为重新创建Random r = new Random();类可能会在某些特定情况下导致问题。阅读它here