Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub Command2_Click()
- For i = 0 To 29
- Command1(i).Caption = Int(100 * Rnd + 1) '使用電腦亂數模擬電池電量
- Command1(i).BackColor = vbGreen
- Next
- '第一題,找出最大值
- x = 0
- For i = 0 To 31
- If Command1(i).Caption > x Then
- x = Command1(i).Caption
- End If
- Next
- Print x
- End Sub
- '第二題,電池插入空格,退出最大的那個電池
- Private Sub Command1_Click(Index As Integer)
- Command1(Index).Caption = 0
- Command1(Index).BackColor = vbGreen
- max_value = 0
- max_index = 0
- For i = 0 To 31
- If Val(Command1(i).Caption) > max_value Then
- 'caption以字串形式儲存,故比較時會出現Bug
- '所以強制轉成數字之後再比較
- max_value = Val(Command1(i).Caption)
- max_index = i
- End If
- Next
- Print max_value
- Command1(max_index).BackColor = &H8000000F
- Command1(max_index).Caption = ""
- End Sub
- Private Sub Command3_Click()
- Timer1.Interval = 1000
- End Sub
- Private Sub Form_Load()
- End Sub
- '第三題,模擬充電
- Private Sub Timer1_Timer()
- For i = 0 To 31
- If Command1(i).BackColor = vbGreen And Command1(i).Caption <> "100" Then
- Command1(i).Caption = Command1(i).Caption + 1
- End If
- Next
- End Sub
- '第四題,修改Command1_Click,解決9x>100的bug
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement