Advertisement
Tony041010

COCORO充電站

Jul 7th, 2021
408
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub Command2_Click()
  2.     For i = 0 To 29
  3.         Command1(i).Caption = Int(100 * Rnd + 1)  '使用電腦亂數模擬電池電量
  4.        Command1(i).BackColor = vbGreen
  5.     Next
  6. '第一題,找出最大值
  7.    x = 0
  8.     For i = 0 To 31
  9.         If Command1(i).Caption > x Then
  10.             x = Command1(i).Caption
  11.         End If
  12.     Next
  13.     Print x
  14. End Sub
  15. '第二題,電池插入空格,退出最大的那個電池
  16. Private Sub Command1_Click(Index As Integer)
  17.     Command1(Index).Caption = 0
  18.     Command1(Index).BackColor = vbGreen
  19.     max_value = 0
  20.     max_index = 0
  21.     For i = 0 To 31
  22.         If Val(Command1(i).Caption) > max_value Then
  23.         'caption以字串形式儲存,故比較時會出現Bug
  24.        '所以強制轉成數字之後再比較
  25.            max_value = Val(Command1(i).Caption)
  26.             max_index = i
  27.         End If
  28.     Next
  29.         Print max_value
  30.         Command1(max_index).BackColor = &H8000000F
  31.         Command1(max_index).Caption = ""
  32. End Sub
  33.  
  34. Private Sub Command3_Click()
  35.     Timer1.Interval = 1000
  36. End Sub
  37.  
  38. Private Sub Form_Load()
  39.  
  40. End Sub
  41.  
  42. '第三題,模擬充電
  43. Private Sub Timer1_Timer()
  44.     For i = 0 To 31
  45.         If Command1(i).BackColor = vbGreen And Command1(i).Caption <> "100" Then
  46.             Command1(i).Caption = Command1(i).Caption + 1
  47.         End If
  48.     Next
  49. End Sub
  50. '第四題,修改Command1_Click,解決9x>100的bug
  51.  
Advertisement
RAW Paste Data Copied
Advertisement