2014-03-24 62 views
1

我試圖產生隨機int數字,一旦我生成它們我想將它們存儲在listBox中,之後將它們排序在第二個listBox之後。的代碼,我有:創建隨機數,將它們存儲在數組中,並將它們排序在兩個列表框中c#

 int Min = 0; 
     int Max = 6; 

     // this declares an integer array with 5 elements 
     // and initializes all of them to their default value 
     // which is zero 
     int[] test2 = new int[6]; 

     Random randNum = new Random(); 
     for (int i = 1; i < test2.Length; i++) 
     { 
      test2[i] = randNum.Next(Min, Max);  
     } 
     arrayListbox.ItemsSource = test2; 
     Array.Sort(test2); 
     foreach (int value in test2) 
     { 
      arrayListboxOrder.ItemsSource = test2; 
     } 
+8

好了,出了什麼問題,您有什麼問題? – Lloyd

+4

以及「完全多餘和不必要的評論獎」的獎項是:「這聲明瞭一個包含5個元素的整數數組,並將它們全部初始化爲其默認值爲零」。此外,它得到了錯誤:它有6個元素,而不是5 –

+0

我注意到評論也是錯誤的... –

回答

1

ItemsSource需要是不同的陣列 - 否則它們都基本上具有相同的數據。排序一個,將它們排序爲「兩個」。

嘗試:

arrayListbox.ItemsSource = test2; 
int[] sorted = (int[])test2.Clone(); 
Array.Sort(sorted); 
arrayListboxOrder.ItemsSource = sorted; 
0
 int Min = 0; 
     int Max = 6; 
     // this declares an integer array with 5 elements 
     // and initializes all of them to their default value 
     // which is zero 
     //int[] test2 = new int[6]; 

     arrayListboxOrder.ItemsSource = Enumerable.Range(Min, Max).OrderBy(x => Guid.NewGuid()).Take(5).OrderBy(n=>n).ToArray(); 

我保存在這裏的一個片段: http://rextester.com/GBM61947

相關問題