自学VB一周了,自编了一个很多人玩过的猜数的游戏,原理是:
电脑随机出一个四位数(每个位数上的数都不相同),游戏者猜数,判断游戏者每个位数上的数和随机数的相同情况,并提示游戏者,如值相同则为B,位置和值都相同则为A,如:随机数为1234,猜测数为3456时,提示0A2B,猜测数为5634时,提示2A0B,达到4A0B则表示猜中。窗体:1个用户输入文本框(textbox1),一个显示提示文本框(textbox2),一个重猜按钮(CommandButton1),一个取消按钮(CommandButton2)
第一次试编出现问题:每点击一次“重猜”,随机数发生了变化,分析原因,随机数生成那一段放在了(CommandButton1)事件中,
第二次试编把随机数生成那一段单独作为一个过程,出现问题:每点击一次“重猜”,提示均为OAOB,分析原因,随机数变量没有被后面的过程引用求教:如何引用另一过程的变量
代码如下:
Private Sub UserForm_Initialize()
Dim one As String '第1个随机数
Dim two As String '第2个随机数
Dim tree As String '第3个随机数
Dim four As String '第4个随机数one = Int((9 - 1 + 1) * Rnd + 1) '得到第1个随机数'得到和第1个随机数不同的第2个随机数
two = Int((9 - 1 + 1) * Rnd + 1)
Do While one = two
two = Int((9 - 1 + 1) * Rnd + 1)
Exit Do
Loop'得到和前2个随机数不同的第3个随机数
tree = Int((9 - 1 + 1) * Rnd + 1)
Do While tree = one Or tree = two
tree = Int((9 - 1 + 1) * Rnd + 1)
Exit Do
Loop'得到和前3个随机数不同的第4个随机数
four = Int((9 - 1 + 1) * Rnd + 1)
Do While four = one Or four = two Or four = tree
four = Int((9 - 1 + 1) * Rnd + 1)
Exit Do
LoopEnd Sub
Private Sub CommandButton1_Click()
Dim zishu As String
zishu = Len(TextBox1.Text)
If zishu <> 4 Then MsgBox "别想骗我,请输入4个数字" Else Call panduan
End Sub
Private Sub panduan()
Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim apanduan As Byte
Dim bpanduan As Byte
Dim cpanduan As Byte
Dim dpanduan As Byte
Dim apan As Byte
Dim bpan As Byte
Dim cpan As Byte
Dim dpan As Byte
Dim ashu As Byte
Dim bshu As Byte
Dim ab As String
Dim abc As String'取得游戏者输入的四个数字
a = Left(TextBox1.Text, Len(TextBox1.Text) - 3)
ab = Left(TextBox1.Text, Len(TextBox1.Text) - 2)
abc = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
b = Right(ab, Len(ab) - 1)
c = Right(abc, Len(abc) - 2)
d = Right(TextBox1.Text, Len(TextBox1.Text) - 3)'判断几个B
If a = two Or a = tree Or a = four Then
apanduan = 1
ElseIf b = one Or b = tree Or b = four Then
bpanduan = 1
ElseIf c = one Or c = two Or c = four Then
cpanduan = 1
ElseIf d = one Or d = two Or d = tree Then
dpanduan = 1
End If
bshu = apanduan + bpanduan + cpanduan + dpanduan'判断几个A
If a = one Then
apan = 1
ElseIf b = two Then
bpan = 1
ElseIf c = tree Then
cpan = 1
ElseIf d = four Then
dpan = 1
End If
ashu = apan + bpan + cpan + dpan'显示几个A几个B
TextBox2.Text = ashu & "A" & ashu & "B"
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
电脑随机出一个四位数(每个位数上的数都不相同),游戏者猜数,判断游戏者每个位数上的数和随机数的相同情况,并提示游戏者,如值相同则为B,位置和值都相同则为A,如:随机数为1234,猜测数为3456时,提示0A2B,猜测数为5634时,提示2A0B,达到4A0B则表示猜中。窗体:1个用户输入文本框(textbox1),一个显示提示文本框(textbox2),一个重猜按钮(CommandButton1),一个取消按钮(CommandButton2)
第一次试编出现问题:每点击一次“重猜”,随机数发生了变化,分析原因,随机数生成那一段放在了(CommandButton1)事件中,
第二次试编把随机数生成那一段单独作为一个过程,出现问题:每点击一次“重猜”,提示均为OAOB,分析原因,随机数变量没有被后面的过程引用求教:如何引用另一过程的变量
代码如下:
Private Sub UserForm_Initialize()
Dim one As String '第1个随机数
Dim two As String '第2个随机数
Dim tree As String '第3个随机数
Dim four As String '第4个随机数one = Int((9 - 1 + 1) * Rnd + 1) '得到第1个随机数'得到和第1个随机数不同的第2个随机数
two = Int((9 - 1 + 1) * Rnd + 1)
Do While one = two
two = Int((9 - 1 + 1) * Rnd + 1)
Exit Do
Loop'得到和前2个随机数不同的第3个随机数
tree = Int((9 - 1 + 1) * Rnd + 1)
Do While tree = one Or tree = two
tree = Int((9 - 1 + 1) * Rnd + 1)
Exit Do
Loop'得到和前3个随机数不同的第4个随机数
four = Int((9 - 1 + 1) * Rnd + 1)
Do While four = one Or four = two Or four = tree
four = Int((9 - 1 + 1) * Rnd + 1)
Exit Do
LoopEnd Sub
Private Sub CommandButton1_Click()
Dim zishu As String
zishu = Len(TextBox1.Text)
If zishu <> 4 Then MsgBox "别想骗我,请输入4个数字" Else Call panduan
End Sub
Private Sub panduan()
Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim apanduan As Byte
Dim bpanduan As Byte
Dim cpanduan As Byte
Dim dpanduan As Byte
Dim apan As Byte
Dim bpan As Byte
Dim cpan As Byte
Dim dpan As Byte
Dim ashu As Byte
Dim bshu As Byte
Dim ab As String
Dim abc As String'取得游戏者输入的四个数字
a = Left(TextBox1.Text, Len(TextBox1.Text) - 3)
ab = Left(TextBox1.Text, Len(TextBox1.Text) - 2)
abc = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
b = Right(ab, Len(ab) - 1)
c = Right(abc, Len(abc) - 2)
d = Right(TextBox1.Text, Len(TextBox1.Text) - 3)'判断几个B
If a = two Or a = tree Or a = four Then
apanduan = 1
ElseIf b = one Or b = tree Or b = four Then
bpanduan = 1
ElseIf c = one Or c = two Or c = four Then
cpanduan = 1
ElseIf d = one Or d = two Or d = tree Then
dpanduan = 1
End If
bshu = apanduan + bpanduan + cpanduan + dpanduan'判断几个A
If a = one Then
apan = 1
ElseIf b = two Then
bpan = 1
ElseIf c = tree Then
cpan = 1
ElseIf d = four Then
dpan = 1
End If
ashu = apan + bpan + cpan + dpan'显示几个A几个B
TextBox2.Text = ashu & "A" & ashu & "B"
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
1.設個全局變量,public <變量名> as <類型>
如: public strA as string '用strA 得到
2.用函數傳遞
sub bbb()
dim strB as string
......
strB=StrA '得到變量中值,運行之後,strB=strA
end sub function strA() as string
........ '運算式
str=调用过程的变量 '需要的變量
end functin3.地址傳遞
sub bbb()
dim strB as string
......
aaa strB '得到變量中值,運行之後,strB=strA
end sub sub aaa(strA as string)
.......
strA=调用过程的变量 '需要的變量 end sub
Dim two As String '第2个随机数
Dim tree As String '第3个随机数
Dim four As String '第4个随机数
定义为全局变量!!
为表示感谢,再加点分。本人有点笨,还是有以下问题:
我试了全局变量,不知我改得对不对,我改成
public one As String '第1个随机数
public two As String '第2个随机数
public tree As String '第3个随机数
public four As String '第4个随机数
提示“sub或fuction中的属性无效”
我再试在通用声明里写以上代码,没提示但也没解决问题。想试下2楼提到的函数传递和地址传递,没看得很懂,因为是第一次编,所以去查找了“帮助”和MSDN,还是不太懂,在过程名里的()里改来改去,还是没解决,烦请2楼再帮下忙,能不能直接在我代码里改改,以便我学习,另外,这程序里还能不能优化,帮我提提意见,先谢谢了。
另外,你可以把string 改成其它變量,Integer
, 如:
Public tree As StringPrivate Sub Command1_Click()
Call ABC
Text3.Text = tree
End SubSub ABC()
Dim one As String
Dim two As String
one = Text1.Text
two = Text2.Text
tree = one & two
End Sub
2.用函數傳遞
Public one As String
Public two As StringPrivate Sub Command1_Click()
one = Text1.Text
two = Text2.Text
Text3.Text = tree
End SubFunction tree() As String
tree = one & two
End Function3.地址傳遞
Private Sub Command1_Click()
Dim tree As String
tree = ""
ABC tree
Text3.Text = tree
End SubSub ABC(tree As String)
Dim one As String
Dim two As String
one = Text1.Text
two = Text2.Text
tree = one & two
End Sub
我试了全局变量,不知我改得对不对,我改成
public one As String '第1个随机数
public two As String '第2个随机数
public tree As String '第3个随机数
public four As String '第4个随机数
提示“sub或fuction中的属性无效”
我再试在通用声明里写以上代码,没提示但也没解决问题。
====================================
所谓全局变量,并 不是你把关键字改成PUBLIC就可以的 ,定义的位置你也要放在任何函数过程以外。
你把你定义的4个全局变量放最上面!
我再一次尝试了各位的方法,没有效果,我想你们的方法不会有问题,我就在每个过程中加入了一个msgbox查看返回的数值,然后运行,结果发现:If a = two Or a = tree Or a = four Then
apanduan = 1
ElseIf b = one Or b = tree Or b = four Then
bpanduan = 1
ElseIf c = one Or c = two Or c = four Then
cpanduan = 1
ElseIf d = one Or d = two Or d = tree Then
dpanduan = 1
End If
bshu = apanduan + bpanduan + cpanduan + dpanduan '判断几个A
If a = one Then
apan = 1
ElseIf b = two Then
bpan = 1
ElseIf c = tree Then
cpan = 1
ElseIf d = four Then
dpan = 1
End If
ashu = apan + bpan + cpan + dpan '显示几个A几个B
TextBox2.Text = ashu & "A" & ashu & "B"
在这一段中始终显示0A0B
我再把变量改为Integer,结果报错,因为这个变量会返回空字符.煩請大家再帮我看看这段到底有什么问题??????????????
如值相同则为B,位置和值都相同则为A1234 第一个数1 第二个数2 第三个数3 第4个数4
3456 第一个数3 第二个数4 第三个数5 第4个数6 根本一个都不同嘛
这个提示0A2B 是如何来的???
向大家汇报一下!!!!!!!
原因是我没设字的顺序,在Private Sub UserForm_Initialize()
......
cccc=one & two & three & four
......
end sub
Private Sub CommandButton1_Click()
......
dddd=a & b & c & d
......
end sub在调试过程中又发现if语句错了,它只判断第一个条件,做了修改解决,
最后调试发现,随机数中有重复数,在DO语句中加了if,问题解决.
至此问题全部解决.
通过这个小编程让我觉得编程很有趣,同时对各种可能出现的情况要考虑周全.
感谢大家的帮忙.
分人人有份.
Public two As String
Public three As String
Public four As String
Public cccc As StringPrivate Sub UserForm_Initialize()one = Int((9 - 1 + 1) * Rnd + 1) '得到第1个随机数'得到和第1个随机数不同的第2个随机数
two = Int((9 - 1 + 1) * Rnd + 1)
Do While one = two
two = Int((9 - 1 + 1) * Rnd + 1)
If one <> two Then Exit Do
Loop'得到和前2个随机数不同的第3个随机数
three = Int((9 - 1 + 1) * Rnd + 1)
Do While three = one Or three = two
three = Int((9 - 1 + 1) * Rnd + 1)
If three <> one And three <> two Then Exit Do
Loop'得到和前3个随机数不同的第4个随机数
four = Int((9 - 1 + 1) * Rnd + 1)
Do While four = one Or four = two Or four = three
four = Int((9 - 1 + 1) * Rnd + 1)
If four <> one And four <> two And four <> three Then Exit Do
Loopcccc = one & two & three & four
MsgBox (cccc)
End Sub
Private Sub CommandButton1_Click()
Dim zishu As String
Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim apanduan As Byte
Dim bpanduan As Byte
Dim cpanduan As Byte
Dim dpanduan As Byte
Dim apan As Byte
Dim bpan As Byte
Dim cpan As Byte
Dim dpan As Byte
Dim ashu As Byte
Dim bshu As Byte
Dim ab As String
Dim abc As String
Dim dddd As Stringzishu = Len(TextBox1.Text)
If zishu <> 4 Then MsgBox "别想骗我,请输入4个数字"'取得游戏者输入的四个数字
a = Left(TextBox1.Text, Len(TextBox1.Text) - 3)
ab = Left(TextBox1.Text, Len(TextBox1.Text) - 2)
abc = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
b = Right(ab, Len(ab) - 1)
c = Right(abc, Len(abc) - 2)
d = Right(TextBox1.Text, Len(TextBox1.Text) - 3)dddd = a & b & c & d
If a = b Or a = c Or a = d Or b = c Or b = d Or c = d Then MsgBox "输入的四个数须不相同"
cccc = one & two & three & four
MsgBox (dddd)
MsgBox (cccc)
'判断几个B
If a = two Or a = three Or a = four Then apanduan = 1
If b = one Or b = three Or b = four Then bpanduan = 1
If c = one Or c = two Or c = four Then cpanduan = 1
If d = one Or d = two Or d = three Then dpanduan = 1
bshu = apanduan + bpanduan + cpanduan + dpanduan
MsgBox (bshu & "B")
'判断几个A
If a = one Then apan = 1
If b = two Then bpan = 1
If c = three Then cpan = 1
If d = four Then dpan = 1
ashu = apan + bpan + cpan + dpan
MsgBox (ashu & "A")'显示几个A几个B
TextBox2.Text = ashu & "A" & bshu & "B"
End SubPrivate Sub CommandButton2_Click()
Unload UserForm1
End Sub