经典小游戏:猜数字
附工程链接
可执行程序
Public Function calc() As Long
Randomize
calc = Int(Rnd * 100) + 1
End Function
Private Sub guess_Click()
' If Val(redo.Tag) = 0 Then result.Caption = "快去重置吧!"
Dim tmp As Long
tmp = Val(read.Text)
read.Text = ""
guess.Tag = Str(Val(guess.Tag) + 1)
If tmp < redo.Tag Then
result.Caption = "Small!"
mini.Caption = Str(tmp)
'If tmp > Val(mini.Caption) Then
ElseIf tmp > redo.Tag Then
result.Caption = "Big!"
maxi.Caption = Str(tmp)
' If tmp < Val(maxi, Caption) Then
Else
result.Caption = "Congratulations!" + Chr(10) + "答案是" + Str(tmp) + Chr(10) + "你只用了" + Str(guess.Tag) + "次机会就猜到了!"
redo.Tag = 0
End If
End Sub
Private Sub redo_Click()
guess.Tag = "0": mini.Caption = "1": maxi.Caption = "100"
Label1.Caption = "~": result.Caption = "": read.Text = ""
redo.Tag = Str(calc())
Do While Val(redo.Tag) < 1 Or Val(redo.Tag) > 100
redo.Tag = Str(calc())
Loop
End Sub