Re: [VB6 ] 新手問題

看板Visual_Basic作者 (闇に沈んだ刃)時間10年前 (2013/08/20 15:03), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串5/5 (看更多)
*************************************** 很久沒有用VB6了 由於沒有實測過,所以有些地方如果有寫錯請見諒 *************************************** 首先是這一段 If a = 2 Then Text1.Text = "電腦-20%血量!" Else If a = 4 Then Text1.Text = "玩家-20%血量!" Else If a = 1 Then Text1.Text = "平手" Else If a = 3 Then Text1.Text = "平手" Else If a = 5 Then Text1.Text = "平手" End If End If End If End If End If 下面有一堆End If 所以排起來是"巢狀" 可是明明是依序Else下來 所以可以不用用到巢狀 If a = 2 Then Text1.Text = "電腦-20%血量!" Else If a = 4 Then Text1.Text = "玩家-20%血量!" Else If a = 1 Then Text1.Text = "平手" Else If a = 3 Then Text1.Text = "平手" Else If a = 5 Then Text1.Text = "平手" End If ***************************************************************************** 接著,雖然是Else If 不過既然只有判定a的值,而a不可能同時有兩種情況 代表這些Else並沒有判定的順序差別 所以可以用Select Case Select Case a Case 2 Text1.Text = "電腦-20%血量!" Case 4 Text1.Text = "玩家-20%血量!" Case 1 Text1.Text = "平手" Case 3 Text1.Text = "平手" Case 5 Text1.Text = "平手" End Select 有沒有覺得跟上面的這段很像? Select Case a Case 1 Text2.Text = "金" Case 2 Text2.Text = "木" Case 3 Text2.Text = "水" Case 4 Text2.Text = "火" Case 5 Text2.Text = "土" End Select 這表示這兩段根本可以寫在一起 Select Case a Case 1 Text1.Text = "平手" Text2.Text = "金" Case 2 Text1.Text = "電腦-20%血量!" Text2.Text = "木" Case 3 Text1.Text = "平手" Text2.Text = "水" Case 4 Text1.Text = "玩家-20%血量!" Text2.Text = "火" Case 5 Text1.Text = "平手" Text2.Text = "土" End Select ******************************************************************* 接著,從判斷來說,可以知道Command1這個按鈕應該是"金" 也就是你必須要寫五個按鈕 但是其中又有很多地方需要重複 這表示你"必須要把他包成函式" 首先先改變一個想法 Select Case a Case 1 Text2.Text = "金" Case 2 Text2.Text = "木" Case 3 Text2.Text = "水" Case 4 Text2.Text = "火" Case 5 Text2.Text = "土" End Select 這個作法,表示你依據a=1~5改變text2的值 不過你可以把你需要的值存成陣列 Dim a as string = ("金","木","水","火","土") 然而為了相剋判定方便,所以先寫成想要的順序 Dim a as string = ("水","土","木","金","火") 其中a(0)="水",而a(1)贏a(0),a(2)贏a(1).....依此類推 接著,用另一個變數i,去取a的值 Text2.text=a(i) ********************************************************************** 假設把按鈕代表的字,也用數字表示 例如按下金的按鈕,便一樣用4代替 此外判定過程用函式表示 那麼程式會變成這個樣子 Private Sub Command1_Click() Call judge(0) '假設按鈕1是水 End Sub Private Sub Command2_Click() Call judge(1) '假設按鈕2是土 End Sub ....... 依此類推 那麼後面會是 Private Sub judge(p) Text2.text=a(i) '秀出電腦的字,電腦的值是i,玩家的值是p If (i>p or i=0 and p=4) Then '後者大於前者,但水>火(0>4) Text1.Text = "玩家-20%血量!" Else If(p>i or p=0 and i=4) Then Text1.Text = "電腦-20%血量!" Else Text1.Text = "平手" End If End Sub *************************************** -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.84.246.136

08/20 17:52, , 1F
你人會不會太好 |D
08/20 17:52, 1F

08/20 17:59, , 2F
人好不錯啊~ 人有時需要一點衝勁。
08/20 17:59, 2F

08/20 23:57, , 3F
用心的大大給推:D
08/20 23:57, 3F
文章代碼(AID): #1I4nJJcR (Visual_Basic)
文章代碼(AID): #1I4nJJcR (Visual_Basic)