春秋航空石家庄营业部:关于一个简单的VB程序的设计

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 08:06:57
为一个电视节目设计一个简单的"猜数游戏"程序

游戏规则:游戏者猜0-99之间的一个整数(这个整数由程序随机产生猜测的次数越少越好.

控件
文本框
Text 供游戏者输入所猜的数(0-99),每输入一个数后需要按回车键.
Text 程序输出与谜底比较的结果(太大.太少.正确)
Text 程序输出游戏者已经猜过的次数.
命令按钮Command 1 游戏重新开始

======================
我不会‘可以帮帮我吗?
谢谢‘

Dim Num1 As Integer
Dim i As Long
Private Sub Command1_Click()
Randomize
Num1 = Int(100 * Rnd)
End Sub

Private Sub Form_Load()
Command1_Click
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim Valuetext As Integer
If (KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And Not KeyAscii = 13 Then
MsgBox "请输入数字!"
KeyAscii = 0
Exit Sub
End If

If KeyAscii = 13 Then
Valuetext = Val(Text1.Text)
i = i + 1
If Valuetext > Num1 Then
MsgBox "你输入的:" & Text1.Text & " 太大!" & vbCrLf & "你已经猜了:" & i & " 次!"
ElseIf Valuetext = Num1 Then
MsgBox "你真利害!你只用了 " & i & " 次,就猜出来了。"
Else
MsgBox "你输入的:" & Text1.Text & " 太小!" & vbCrLf & "你已经猜了:" & i & " 次!"

End If
Text1.Text = ""
End If

End Sub