99V久久综合狠狠综合久久|久久se无码精品一区二区|99国产精美欧美一区二区|久久99国产精品亚洲

VB小程序,猜數(shù)字游戲!

2021-02-27 06:40:54  閱讀:-  來(lái)源:

VB小程序,關(guān)注我有更多的有趣的VB小程序

實(shí)現(xiàn)功能:程序啟動(dòng)時(shí)隨機(jī)產(chǎn)生1個(gè)到500之內(nèi)的正整數(shù),在Textbook框內(nèi)輸入你猜的數(shù)字,試試看你能幾次猜中,如果10次沒(méi)有猜中,Command按鈕就變換灰色

控件:一個(gè)TextBox文本框,兩個(gè)Label標(biāo)簽,一個(gè)Command命令按鈕

界面:

VB小程序,猜數(shù)字游戲

VB小程序,猜數(shù)字游戲

代碼:

Option Explicit

Dim num As Integer

Dim i As Integer

Private Sub Command1_Click()

If i > 10 Then

Command1.Enabled = False

MsgBox "你已猜了多次,游戲結(jié)束,沒(méi)有猜中"

Exit Sub

End If

If Text1.Text = "" Or Not IsNumeric(Text1.Text) Then‘text1為空或者非數(shù)字就進(jìn)行提示

MsgBox "請(qǐng)輸入數(shù)字!"

Else

If Val(Text1.Text) > num Then

Label2.Caption = "太大了,請(qǐng)重新輸入!"

Text1.Text = ""

i = i + 1

ElseIf Val(Text1.Text) = num Then

Label2.Caption = "恭喜你,你猜對(duì)了!" & "你共猜了" & Str(i) & "次"

i = i + 1

Else

Label2.Caption = "太小了,請(qǐng)重新輸入!"

i = i + 1

Text1.Text = ""

Text1.SetFocus

End If

End If

End Sub

Private Sub Form_Load()

Command1.Caption = "猜數(shù)"

Randomize’每次啟動(dòng)時(shí)生成的隨機(jī)數(shù)不一樣

i = 0

num = Int(Rnd * 500) + 1‘生成隨機(jī)數(shù)

End Sub

想得到更多的VB小程序,請(qǐng)關(guān)注我,點(diǎn)擊下方的廣告可以更好的支持和鼓勵(lì)我哦!