如何修改随机出来的不相同数添加到int数组中,代码如下
private void button1_Click(object sender, EventArgs e){
int x0 = Int32.Parse(textBox3.Text);//最小值
int x1 = Int32.Parse(textBox1.Text);//最大值
int x2 = Int32.Parse(textBox2.Text);//取值个数
listBox1.Items.Clear(); //listbox刷新
int[] num = new int;
for (int i = 0; i < num.Length; i++)
{
Random rand = new Random();
int n;
n = rand.Next(x0, x1);
if (n == num) { }
else
{
num = n;
}
listBox1.Items.Add(num);
}
}
如果你用.net4.0开发的话 会有一个linq特性, 数组能点出来一个函数直接能去重复 程序貌似是正确的,可以尝试多个值测试
页:
[1]