vb猜数字游戏编程 要求:让电脑猜一个0-100的数字(由玩家想好) 直到猜对为止 要用

Dim state As Integer '状态 0表示没开始 1表示已开始

Dim min As Integer '最小值

Dim max As Integer '最大值

Dim now As Integer '中值

Dim c As Integer '猜的次数

Private Sub Command1_Click() '开始

state = 1

Guess

End Sub

Private Sub Command2_Click() '小了

If state = 0 Then MsgBox "还没开始呢!": Exit Sub

min = now

Guess

End Sub

Private Sub Command3_Click() '大了

If state = 0 Then MsgBox "还没开始呢!": Exit Sub

max = now

Guess

End Sub

Private Sub Command4_Click() '正确

If state = 0 Then MsgBox "还没开始呢!": Exit Sub

state = 0

MsgBox "哇哇,我太厉害了,才猜" & c & "次就猜中了!"

End Sub

Private Sub Form_Load()

state = 0

min = 0

max = 100

c = 0

End Sub

Function GetMid() As Integer '取得中值

GetMid = (max - min) / 2 + min

End Function

Sub Guess() '电脑猜测

now = GetMid

c = c + 1

MsgBox "我猜是" & now & "!"

End Sub