我有两个数组,一个是 PictureBox 数组,另一个是 Integer 数组,它们都具有相同数量的元素。我希望两个数组每次都随机洗牌,但都以同样的方式洗牌。
如果我使用两个 PictureBox 数组或两个 Integer 数组,这是一个有效的代码,但是我希望它对一个 PictureBox 数组和一个 Integer 数组进行洗牌。
这是我想要工作的代码:(两个不同的数组)
PictureBox[] cards = new PictureBox[13];
// PictureBox[] numValue = new PictureBox[13];
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
Random rnd = new Random();
for (int i = 0; i < cards.Length - 1; i++)
{
int j = rnd.Next(i, cards.Length);
PictureBox temp = cards[j];
cards[j] = cards[i];
cards[i] = temp;
temp = numbers[j]; //An error occurs here because numbers is not a PictureBox variable
numbers[j] = numbers[i];
numbers[i] = temp;
}
这是有效的代码:(对于相同的数组)
PictureBox[] numValue = new PictureBox[13];
PictureBox[] cards = new PictureBox[13];
// int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
Random rnd = new Random();
for (int i = 0; i < cards.Length - 1; i++)
{
int j = rnd.Next(i, cards.Length);
PictureBox temp = cards[j];
cards[j] = cards[i];
cards[i] = temp;
temp = numValue [j];
numValue [j] = numValue [i];
numValue [i] = temp;
}
如果您知道另一个可以帮助我的不同代码,请随时分享!