2013-11-09 107 views
0

我正在寫一個程序,它創建一個隨機數並移動到一個數組。這是我的課程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()); 
     } 

最小值和最大值是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    
     }  
    } 

我的問題是我怎麼能要求我的陣列和隨機數到我的按鈕1。

現在我有錯誤'不能隱式地將類型轉換爲int'在第一個FOR循環中。

感謝和問候:)

+1

我建議你將所有的邏輯移入'Randomize',並且只給構造函數提供計數和隨機限制。 – ja72

回答

1
Randomize[] array= new Randomize[10]; 

本來應該

int[] array = new int[10]; 
1

問題

錯誤是本着

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

rand.Next(0,100);給出了一個整數,你不能從int轉換爲隨機。那是什麼錯誤在說。

'cannot implicitly convert type to int'

解決方案

,您應該使用整數數組這樣

int[] array= new int[10]; 
+0

是的,謝謝。但我的數組需要排序。這是下一個問題。我想在這個相同陣列上的下一個按鈕上操作。怎麼做 ?我需要請求這個相同的數組到下一個按鈕。 此致 – user2971920

0

哇這裏有些問題是有問題的。你的班級應該擁有這些數據,並處理其生成和顯示。按鈕事件中不應有任何操作,除非指示您的班級顯示數據。除非聲明和描述爲const成員,否則您應該在課程中沒有幻數,如10100

舉一個例子來看看下面的代碼,看看你是否已經弄清楚它是如何與你的代碼(在什麼地方做什麼)不同。

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(); 
    }