我使用C#創建一個紙牌遊戲。我想爲我的卡片示例分配一個值:Ace(image)= 1;我想隨機選擇它。這裏是我的代碼:C#在圖片框中的隨機圖像,並分配值
private void button1_Click(object sender, EventArgs e)
{
Random cards = new Random();
card = cards.Next(0, 9);
switch (card)
{
case 0:
pictureBox1.Image = Properties.Resources.king_d;
pictureBox2.Image = Properties.Resources.jack_s;
break;
case 1:
pictureBox1.Image = Properties.Resources.ace_c;
pictureBox2.Image = Properties.Resources.ten_d;
break;
}
}
}
您應該加載圖像到一個數組一次,然後從分配給pictureboxes,或者以前的圖像處置,如果有的話, – Plutonix