风龙天泉 发表于 2014-5-7 13:38:30

如何修改随机出来的不相同数添加到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);
            }
      }

ibcadmin 发表于 2014-5-7 14:33:17

如果你用.net4.0开发的话    会有一个linq特性,   数组能点出来一个函数直接能去重复

飞/可爱朋 发表于 2014-5-7 23:11:23

程序貌似是正确的,可以尝试多个值测试
页: [1]
查看完整版本: 如何修改随机出来的不相同数添加到int数组中,代码如下