我想獲得一張卡片的隨機圖片生成器。我正在嘗試使用數組爲五個圖片框生成五個不同的圖片。我目前使用if語句,但想要使用數組。這是到目前爲止我的代碼,如何在陣列中生成隨機圖片
Dim rndnumber As Integer
Randomize()
rndnumber = Int(Rnd() * 52) + 1
If ListBox1.Items.Contains(rndnumber) Then
rndnumber = Int(Rnd() * 52) + 1
End If
ListBox1.Items.Add(rndnumber)
If rndnumber = 1 Then
Me.playerhand1.Image = My.Resources.aceclubs
ElseIf rndnumber = 2 Then
Me.playerhand1.Image = My.Resources.acediamonds
ElseIf rndnumber = 3 Then
Me.playerhand1.Image = My.Resources.acehearts
ElseIf rndnumber = 4 Then
Me.playerhand1.Image = My.Resources.acespades
ElseIf rndnumber = 5 Then
Me.playerhand1.Image = My.Resources._2clubs
ElseIf rndnumber = 6 Then
Me.playerhand1.Image = My.Resources._2diamonds
ElseIf rndnumber = 7 Then
Me.playerhand1.Image = My.Resources._2hearts
ElseIf rndnumber = 8 Then
Me.playerhand1.Image = My.Resources._2spades
ElseIf rndnumber = 9 Then
Me.playerhand1.Image = My.Resources._3clubs
ElseIf rndnumber = 10 Then
Me.playerhand1.Image = My.Resources._3diamonds
ElseIf rndnumber = 11 Then
Me.playerhand1.Image = My.Resources._3hearts
ElseIf rndnumber = 12 Then
Me.playerhand1.Image = My.Resources._3spades
ElseIf rndnumber = 13 Then
Me.playerhand1.Image = My.Resources._4clubs
ElseIf rndnumber = 14 Then
Me.playerhand1.Image = My.Resources._4diamonds
ElseIf rndnumber = 15 Then
Me.playerhand1.Image = My.Resources._4hearts
ElseIf rndnumber = 16 Then
Me.playerhand1.Image = My.Resources._4spades
ElseIf rndnumber = 17 Then
Me.playerhand1.Image = My.Resources._5clubs
ElseIf rndnumber = 18 Then
Me.playerhand1.Image = My.Resources._5diamonds
ElseIf rndnumber = 19 Then
Me.playerhand1.Image = My.Resources._5hearts
ElseIf rndnumber = 20 Then
Me.playerhand1.Image = My.Resources._5spades
End If
End Sub
當你做,你遇到什麼問題,該指數開始?正如你已經注意到的那樣,使用數組或字典來擺脫你的多個if-else語句要好得多 – Ian
問題是,我希望它是這樣5個不同的圖片框獲得不同的圖像。到目前爲止,所有的代碼只是一個圖片框。 –
[This answer](http://stackoverflow.com/a/26532939/1070452)可能會讓人感興趣,它將更容易從ImageList中讀取圖像,您可以通過從套裝中創建的密鑰引用它們並排名 – Plutonix