帮我用vb编写一个猜数字的小游戏,数字1到100,程序语言3到5行即可.

2024-12-29 08:08:09
推荐回答(4个)
回答1:

画个text1和command1

Private Sub Command1_Click()
Dim a As Long
a = Rnd * 100
MsgBox IIf(a = Val(Text1), "你猜对了,数字是:" & a, "你猜错了,正确答案是:" & a)
End Sub

回答2:

做command两个,textbox一个。
下面为代码:

Dim a as integer
Private Sub Form_Load()
command1.caption="Guess"(即确认)
command1.enabled= False
command2.caption="Reset"(重新生成数字)
End Sub

Private Sub Command1_Click()
If val(text1.text)=a then
Msgbox "Correct!"
Else
Msgbox "Incorrect! Answer is: " & a
Endif
command1.enabled=false
call command2_click()
End Sub

Private Sub Command2_Click()
a=int(rnd()*100)+1
command1.enabled=true
msgbox " New number generated!"
End Sub

回答3:

猜数字的小游戏:前提,随机数在你猜中之前不能变
所以要建一个生成新数字按钮command2
如果不想猜又想答案的话还要加一个command3显示答案,这里就显示在text1里
private sub command2click()
x= Int(Rnd() * 100)
end sub
Private Sub Command1_Click()
if val(text1.text)>x then
msgbox "太大了"
elseif val(text1.text)msgbox "太小了"
else
msgbox "你太棒了!这么快就猜对了"
end if
End Sub

private sub command3_click()
text1.text=x
end sub

回答4:

3楼的回答比较好,而且做出来会有意思点!