VB猜数字游戏

求高手修改 谢谢
Dim A1, B1, C1, D1 As Integer
Dim a, b, c, d As Integer

Private Sub Command1_Click()
Text1.Visible = True
Text2.Visible = True
Text3.Visible = True
Text4.Visible = True
A1 = Int(9 * Rnd)
Do
B1 = Int(9 * Rnd)
Loop While B1 = A1
Do
C1 = Int(9 * Rnd)
Loop While C1 = A1 Or C1 = B1
Do
D1 = Int(9 * Rnd)
Loop While D1 = A1 Or D1 = B1 Or D1 = C1
Text1 = Clear
Text2 = Clear
Text3 = Clear
Text4 = Clear
Label1 = Clear
Label2 = Clear
End Sub
Private Sub Command2_Click()
Dim X, Y As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
d = Val(Text4.Text)
If a = A1 Then
X = X + 1
ElseIf a = B1 Or a = C1 Or a = D1 Then
Y = Y + 1
End If
If b = B1 Then
X = X + 1
ElseIf b = A1 Or b = C1 Or d = D1 Then
Y = Y + 1
End If
If c = C1 Then
X = X + 1
ElseIf c = A1 Or c = B1 Or c = D1 Then
Y = Y + 1
End If
If d = D1 Then
X = X + 1
ElseIf d = A1 Or d = B1 Or c = C1 Then
Y = Y + 1
End If
If Text1.Text = Text2.Text Or Text1.Text = Text3.Text Or Text1.Text = Text4.Text Or Text2.Text = Text3.Text Or Text2.Text = Text4.Text Or Text3.Text = Text4.Text Then
Label1.Caption = "禁止重复"
Else: Label1.Caption = X & "X" & Y & "Y"
End If
End Sub

Private Sub Command3_Click()
Label2 = A1 & B1 & C1 & D1
End Sub

在使用rnd函数之前,要先用randomize语句初始化,rnd函数就会以系统时间作为种子,这样每次生成的随机序列就不同了。还有啊,rnd返回大于等于0小于1的单精度数值,而int只是取整,不会四舍五入,所以要用int(10*rnd)才能得到0到9的整数,9*rnd只能得到0到8的整数。赋值的时候你要注意数据类型,label是字符串的,a1是整数,因此&在那里是与运算符,而不是字符串连接符。
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-02-27
Private Sub Command1_Click()
If Command1.Caption = "猜猜看(&OK)" Then
Label1.Caption = "猜猜看,1-99它会是什么数"
mm:
Command1.Caption = "开始(&OK)"
bb = InputBox("请输入1个2位数的数字", "猜猜看", "00")
If Not IsNumeric(bb) Or Not bb <> "" Then
MsgBox "输入的不是数字或没有输入", 32, "友情提示"
GoTo mm
End If
If bb >= 100 Then
MsgBox "你输入的数大于100", 32, "友情提示"
GoTo mm
End If
End If
If Command1.Caption = "开始(&OK)" Then
For b = 1 To 2000
Randomize
a = Int(Rnd * 9 + 1)
c = Int(Rnd * 9)
Label3.Caption = ""
Text1.PasswordChar = "*"
Text1.Text = "" & a & "" & c & ""
Label1.Caption = "" & a & "" & c & ""
DoEvents
Next
Command1.Caption = "猜猜看(&OK)"
Text1.PasswordChar = ""
If Text1.Text = bb Then
Label1.Caption = "厉害,这都被你猜到了!"
am = Int(Rnd * 3 + 1)
If am = 1 Then
Label3.Caption = "新年快乐!祝你年年有今日,岁岁有今朝.平安无事,万事大吉@_@!!"
End If
If am = 2 Then
Label3.Caption = "哈哈,祝你年年鱼,岁岁平安!"
End If
If am = 3 Then
Label3.Caption = "运气真的很不错!"
End If
Else
Label1.Caption = "哈哈,你猜错了!你输入的是" & bb & ""
ai = Int(Rnd * 3 + 1)
If ai = 1 Then
Label3.Caption = "哈哈,你今天的运气不是一般的倒霉哦!"
End If
If ai = 2 Then
Label3.Caption = "嗯,你今天果然踩了狗屎运,手气很不错!一定要去买彩票!"
End If
If ai = 3 Then
Label3.Caption = "今年你会很倒霉的!!O(∩_∩)O~"
End If
End If
End If
End Sub
Private Sub Form_Load()
Command1.Caption = "猜猜看(&OK)"
Me.Caption = "猜数字"
End Sub

这是我做的程序,缩短了很多代码

用VB语言编写一个猜数字的游戏
首先说一说猜数字这个游戏的玩法,一开始计算机会随机产生一个不重复的四位数,你要输入四位不重复的数与计算机给出的数作对比,如果与计算机给出的数的位置相同数字相同,那么将会是1A,如果数字相同而位置相不同,将会显示1B。例如:计算机的随机数字为:1234 ,我猜的数字为:1356 ,那么这时计算机...

VB猜字游戏程序代码
loop if win=1 then wscript.echo "你猜对了。" else wscript.echo "你没猜对。"\/\/\/ 上面是我写过的一个小游戏。把斜线以上的部分复制到一个文档中,后缀名改为*.vbs,直接双击试试看效果:)参考资料:我的经历

求助!关于用vb写猜数字游戏的代码!网上难找到符合要求的!
Private Sub Command1_Click()Dim n As Integer, i As Integer, a As IntegerRandomizen = Int(Rnd * 100) + 1For i = 1 To 10 a = Val(InputBox("请猜:")) If a = n Then MsgBox "猜中了,恭喜你!" Exit Sub ElseIf a < n Then MsgBox "太小了,继续...

VB中猜数字游戏请高手帮忙啊。
As Integer Dim a As Integer Randomize (10)a = Rnd(10)10 For i = 1 To 3 s = CInt(InputBox("请输入猜测数字!"))If a = s Then MsgBox ("恭喜你猜对了")End End If If a > s Then MsgBox ("对不起,你猜小了!")End If If a < s Then MsgBox ("对不起,你猜大了!"...

vb猜数字游戏程序编写
vb猜数字游戏程序编写 20 编写的时候遇到了几个问题,希望可以帮我解决一下 1.怎样用随机函数产生一个1~10之间的整数 2.怎么才能使在输入三次数字后结束程序。 以下是我的代码 Private Sub Command1_Click() Dim x As Integer, y As Integer, z As Integer Dim myvalue myv... 展开 ...

VB猜数字游戏源代码?
Public Class Form1 Dim b As Integer Dim js As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Me.Button1.Text = "开始" Then Dim a As Integer = MsgBox("游戏开始你准备好了吗", MsgBoxStyle.YesNo, "提示...

帮我用vb编写一个猜数字的小游戏,数字1到100,程序语言3到5行即可...
画个text1和command1 Private Sub Command1_Click()Dim a As Long a = Rnd 100 MsgBox IIf(a = Val(Text1),"你猜对了,数字是:"& a,"你猜错了,正确答案是:"& a)End Sub

vb编的猜数字小游戏……不管输入什么数字都显示“数字太大”……求...
Private Sub Form_Load() Randomize a = Int(100 * Rnd + 1)Print "正确答案是:" & a End Sub Private Sub Command1_Click() Dim b As Integer b = Val(InputBox("请输入一个数字(1-100)"))If b > 100 Or b < 1 Then MsgBox ("输入错误!")ElseIf b = a Then MsgBox...

vb里面猜数字游戏问题,新手寻解答??
并且默认初始值为0,解决的办法是把S定义为静态的情况变量,如下:Dim s As Integer 换成 Static s As integer 就可以了。答错了扣分就是在猜错了的判断语句后加上 s -= 10 其中10是你想扣的分,如果你用的VB是6.0的就换成 s = s - 10 ...

VB猜数字游戏!
b = Val(InputBox("请输入一个数字(1-100)", "猜数字"))Select Case b Case Is > 100 Or b < 1:MsgBox "请输入正确的值!"Exit Sub Case c:txt.Text = b & " 恭喜你猜对了!"MsgBox txt.Text, 0, "恭喜"Case Is > c:txt.Text = b & " 太大了!"MsgBox txt.Text, 0, "...

相似回答