我正在尝试生成随机int
数字,一旦我生成它们,我想将它们存储在listBox
中,然后将它们排在第二个listBox
中。我的代码:
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;
}
答案 0 :(得分:1)
ItemsSource
需要是一个不同的数组 - 否则它们基本上都有相同的数据。排序一,排序&#34;两者&#34;。
尝试:
arrayListbox.ItemsSource = test2;
int[] sorted = (int[])test2.Clone();
Array.Sort(sorted);
arrayListboxOrder.ItemsSource = sorted;
答案 1 :(得分: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