概述
满意答案
hhhhot
2013.08.18
采纳率:53% 等级:12
已帮助:4298人
Option Explicit
Dim I随机(4) As Integer
Dim Upper As Integer, Lower As Integer
Private Sub Command1_Click()
Call F产生随机数
End Sub
Private Sub Command2_Click()
Call F猜字
End Sub
Private Sub Form_Load()
Text1.Text = ""
Command1.Caption = "产生随机四位数"
Call F产生随机数
End Sub
Function F产生随机数()
Dim a As Integer
Upper = 9
Lower = 0
a = Int((Upper - Lower + 1) * Rnd + Lower)
I随机(1) = a
a = Int((Upper - Lower + 1) * Rnd + Lower)
I随机(2) = a
a = Int((Upper - Lower + 1) * Rnd + Lower)
I随机(3) = a
a = Int((Upper - Lower + 1) * Rnd + Lower)
I随机(4) = a
Do While F判断是否重复(I随机(2), 1) = True
a = Int((Upper - Lower + 1) * Rnd + Lower)
I随机(2) = a
Loop
Do While F判断是否重复(I随机(3), 2) = True
a = Int((Upper - Lower + 1) * Rnd + Lower)
I随机(3) = a
Loop
Do While F判断是否重复(I随机(4), 3) = True
a = Int((Upper - Lower + 1) * Rnd + Lower)
I随机(4) = a
Loop
'Dim i As Integer
'For i = 1 To 4
' Print I随机(i)
'Next
End Function
Function F判断是否重复(a As Integer, c As Integer) As Boolean
Dim I As Integer
For I = 1 To c
If a = I随机(I) Then
F判断是否重复 = True
Exit Function
End If
Next
F判断是否重复 = False
End Function
Private Sub Text1_Change()
If Len(Text1.Text) = 4 Then Call F猜字
End Sub
Function F猜字()
Dim I As Integer, j As Integer, B猜对个数 As Integer, B猜对数字 As Integer
For I = 1 To 4
If Val(Mid(Text1.Text, I, 1)) = I随机(I) Then B猜对个数 = B猜对个数 + 1
Next
For I = 1 To 4
'Print Mid(Text1.Text, i, 1) & "," & Str(I随机(i))
If Val(Mid(Text1.Text, I, 1)) <> I随机(I) Then
For j = 1 To 4
If Val(Mid(Text1.Text, I, 1)) = I随机(j) Then B猜对数字 = B猜对数字 + 1
Next
End If
Next
Print "猜对个数:" & B猜对个数 & "猜对数字位置不对:" & B猜对数字
Print "原数是:" & I随机(1) & I随机(2) & I随机(3) & I随机(4) & "你猜的是:" & Text1.Text
End Function
00分享举报
最后
以上就是美好小海豚为你收集整理的VB设计一个猜字游戏 计算机随机,vb 猜字游戏程序设计的全部内容,希望文章能够帮你解决VB设计一个猜字游戏 计算机随机,vb 猜字游戏程序设计所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复