目的:设计一个猜数字的游戏
用到的语句:循环、条件
代码如下:
Private Sub Command1_Click()Dim guess, rd, counter As IntegerRandomizerd = Rnd * 100counter = 0Do
guess = Val(Text1.Text)
If counter <= 5 Then
If guess < rd Then
MsgBox " 你输入的数字有点小哦,机会还有4-" & counter
counter = counter + 1
ElseIf guess > rd And counter <= 5 Then
MsgBox "你输入的数字有点大哦,机会还有4-" & counter
counter = counter + 1
End If
Else
MsgBox "对不起,你的次数已到,下次加油哦"
End If
Loop While guess <> rd And counter < 5If guess = rd Then MsgBox "恭喜你回答正确!"
End If
End Sub
问题
1.所猜的数字只能输入一次,怎么改进才能再次输入呢?
2.MsgBox "你输入的数字有点大哦,机会还有4-" & counter 这句怎么改才能智能点呢?不会出现4-0、4-1之类的。
用到的语句:循环、条件
代码如下:
Private Sub Command1_Click()Dim guess, rd, counter As IntegerRandomizerd = Rnd * 100counter = 0Do
guess = Val(Text1.Text)
If counter <= 5 Then
If guess < rd Then
MsgBox " 你输入的数字有点小哦,机会还有4-" & counter
counter = counter + 1
ElseIf guess > rd And counter <= 5 Then
MsgBox "你输入的数字有点大哦,机会还有4-" & counter
counter = counter + 1
End If
Else
MsgBox "对不起,你的次数已到,下次加油哦"
End If
Loop While guess <> rd And counter < 5If guess = rd Then MsgBox "恭喜你回答正确!"
End If
End Sub
问题
1.所猜的数字只能输入一次,怎么改进才能再次输入呢?
2.MsgBox "你输入的数字有点大哦,机会还有4-" & counter 这句怎么改才能智能点呢?不会出现4-0、4-1之类的。
Randomizerd = Int(Rnd * 100)counter = 0
Text1.Text = rd
Do
guess = Val(InputBox("", "请输入一个数字!"))
If counter < 5 Then
If guess < rd Then
MsgBox " 你输入的数字有点小哦,机会还有" & (4 - counter) & "次"
counter = counter + 1
ElseIf guess > rd And counter <= 5 Then
MsgBox "你输入的数字有点大哦,机会还有" & (4 - counter) & "次"
counter = counter + 1
End If
Else
MsgBox "对不起,你的次数已到,下次加油哦"
End If
Loop While guess <> rd And counter < 5If guess = rd Then MsgBox "恭喜你回答正确!"
End If
End Sub