随机数到数组

时间:2013-11-09 13:25:31

标签: c# arrays random

我正在编写一个程序,它创建一个随机数并移动到一个数组。这是我的班级名为randomize:

class Randomize
{
        public int[] _array;
        public int[] Array{ get { return _array; } set { _array= value; } }
        public Randomize(int[] array)
        {
            Array= array;
        }

        public int _min;
        public int Min
        {
            get { return _min; }
            set { _min = value; }
        }
        public int _max;
        public int Max { get { return _max; } set { _max = value; } }

        public Randomize(int min, int max)
        {
            Min = min;
            Max = max;
        }
        public override string ToString()
        {
            return string.Format(Max.ToString(), Min.ToString());
        }
        public override string ToString()
        {
            return string.Format(Array.ToString());
        }

Min和Max是MinValue和MaxValue。

现在我的表格:

 private void button1_Click(object sender, EventArgs e)
        {
            Randomize min = new Randomize(0, 100);
            Random rand= new Random(); // randomize
            Randomize[] array= new Randomize[10]; 
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rand.Next(0,100); //draw in loop
            }
            textBox1.Clear(); 
            for (int i = 0; i < array.Length; i++)
            {


            textBox1.Text = textBox1.Text + " " + array[i].ToString(); //show in textbox              
        }    
    }

我的问题是如何向我的button1请求我的数组和随机数。

现在我有错误'无法在第一个FOR循环中隐式地将类型转换为int。

谢谢和问候:)

3 个答案:

答案 0 :(得分:1)

<强>问题

错误在行

array[i] = rand.Next(0,100);

rand.Next(0,100);给出一个整数,你不能从int转换为Randomize。那是什么错误。

'cannot implicitly convert type to int'

<强>解决方案

你应该使用像这样的整数数组

int[] array= new int[10];

答案 1 :(得分:1)

 Randomize[] array= new Randomize[10]; 

应该是

 int[] array = new int[10];

答案 2 :(得分:0)

哇,这里有一些问题。您的类应拥有数据,并处理其生成和显示。除了指示您的班级显示数据之外,按钮事件中不应该有任何操作。此外,您应该在类中没有像10100这样的幻数,除非它们被声明并描述为const成员。

作为示例,请查看下面的代码,看看您是否已经弄清楚它与您的代码有何不同(就何处和做了什么而言)。

public class RandomArray 
{
    /// <summary>
    /// create a single random number generator
    /// </summary>
    static readonly Random generator = new Random();
    /// <summary>
    /// here are the random numbers stored
    /// </summary>
    int[] array;
    /// <summary>
    /// store the min, max used to generate the data
    /// </summary>
    readonly int min, max;
    /// <summary>
    /// Constructor only needs how the value limits
    /// </summary>
    /// <param name="min">The minimum value (typical 0)</param>
    /// <param name="max">The maximum value (example 100)</param>
    public RandomArray(int min, int max)
    {
        this.min=min;
        this.max=max;
        this.array=new int[0];
    }
    /// <summary>
    /// Fills the array with random numbers
    /// </summary>
    /// <param name="count">The number of data to generate</param>
    public void Fill(int count)
    {
        this.array=new int[count];
        // fill array with random integers
        for (int i=0; i<array.Length; i++)
        {
            array[i]=generator.Next(min, max);
        }
    }        
    /// <summary>
    /// Copy constructor if needed (optional)
    /// </summary>
    /// <param name="other">A RandomArray to copy the data from</param> 
    public RandomArray(RandomArray other)
    {
        this.min=other.min;
        this.max=other.max;
        this.array=(int[])other.array.Clone();
    }
    /// <summary>
    /// Provide the data
    /// </summary>

    public int[] Array { get { return array; } }
    /// <summary>
    /// Provide the limits used
    /// </summary>

    public int Min { get { return min; } }
    public int Max { get { return max; } }
    /// <summary>
    /// Creates a comma separated list of numbers like <c>[45,32,64,..]</c>
    /// </summary>
    public string ToStringList()
    {
        string[] parts=new string[array.Length];
        for (int i=0; i<parts.Length; i++)
        {
            parts[i]=array[i].ToString();
        }
        return "["+string.Join(",", parts)+"]";
    }
    /// <summary>
    /// Shows only the limits used
    /// </summary>
    public override string ToString()
    {
        return string.Format("RandomArray({0},{1})", min, max);
    }
}

    // Click Event
    private void button1_Click(object sender, EventArgs e)
    {
        RandomArray random_array=new RandomArray(0, 100);
        random_array.Fill(10);
        textBox1.Text=random_array.ToStringList();
    }
相关问题