vb 猜字游戏程序设计
PrivateSubCommand1_Click()DimiAsInteger,jAsInteger,kAsIntegerRandomizej=Fix(Rnd*99)+1'PrintjDok=InputBox("请输入你猜的数:","提示",0)Ifk>jThenPrint"大了"ElseIfk150ThenExitDoLoopUntilj=kEndSub
先说程序的错误
1.把Int((10 * Rnd) + 1)放到CMD1里
也就是每点一次产生的数都不一样
怎么猜啊
2.没加计数器,不能判断出猜了几次
所以修改后的代码:
加一个按钮CMD2,用来重新产生随机数,进行新一轮猜数
Dim x As Integer, y As Integer, z As Integer
Dim cishu As Integer '保存次数的变量
Private Sub Command1_Click()
’如果猜3次了就告诉数字是多少,然后退出,等待点击CMD2重新进行
If cishu = 3 Then Msgbox "已经猜3次了,这个数是" & x: Exit Sub
y = Val(Text1.Text)
If Text1.Text = "" Or Not IsNumeric(Text1.Text) Then
MsgBox "输入有误!请重新输入数字", 48
Text1.Text = ""
Text1.SetFocus
Exit Sub
End If
cishu = cishu + 1
Select Case x - y
Case Is < 0
z = MsgBox("你猜数大了,请重猜。注意:你只有三次机会", 48 + 1)
Text1.Text = ""
Text1.SetFocus
Case Is > 0
z = MsgBox("你猜数小了,请重猜。注意:你只有三次机会", 48 + 1)
Text1.Text = ""
Text1.SetFocus
Case Else
z = MsgBox("恭喜你猜中了!", 1)
End Select
End Sub
Private Sub Command2_Click()
‘点CMD2时重新开始,所以重置次数为0,重新产生一个随机数
cishu = 0
Randomize Timer
x = Int((Rnd * 10) + 1)
End Sub
Private Sub Form_Load()
’程序开始时自动重置一次,可以省去,如果省去的话开始前需要点CMD2开始游戏
cishu = 0
Randomize Timer
x = Int((Rnd * 10) + 1)
End Sub
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
兴倪帕尼: PrivateSubCommand1_Click()DimiAsInteger,jAsInteger,kAsIntegerRandomizej=Fix(Rnd*99)+1'PrintjDok=InputBox("请输入你猜的数:","提示",0)Ifk>jThenPrint"大了"ElseIfk150ThenExitDoLoopUntilj=kEndSub
高密市13544742139: 帮我用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
高密市13544742139: 怎样用vb来做一个文字猜谜游戏?
兴倪帕尼: Private Sub Command1_Click() Text1.Text = "天鹅湖边鸟飞绝" Text2.Text = "良无一点双人行" Text3.Text = "双木非林心相随" Text4.Text = "您若无心自先行" Text5.Text = "提示:请猜四个字(一句话里包含一个字)"End Sub Private Sub Command2_Click() If Len(Text6.Text) >= 4 Then If Text6.Text = "" Then MsgBox "" Else MsgBox "" End If End If End Sub
高密市13544742139: VB数字猜猜猜的设计代码 - ?
兴倪帕尼: 我的建议是可以把游戏设计成一个时间限制,不同位数的数有不同的时间 时间倒计时:(以30秒为例) Private Sub Form_Load() Label1.Caption = 30 a = 30 Timer1.Interval = 1000 End Sub Private Sub Timer1_Timer() a = a - 1 Label1.Caption ...
高密市13544742139: 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("游戏开...
高密市13544742139: vb猜数字游戏程序编写 - ?
兴倪帕尼: 先说程序的错误1.把Int((10 * Rnd) + 1)放到CMD1里 也就是每点一次产生的数都不一样 怎么猜啊2.没加计数器,不能判断出猜了几次 所以修改后的代码:加一个按钮CMD2,用来重新产生随机数,进行新一轮猜数 Dim x As Integer, y As Integer, ...
高密市13544742139: 急求VB编写的猜数字游戏代码 - ?
兴倪帕尼: RandomizeDim a As IntegerDim b As Integera = Int(Rnd * 100)s1 = "请输入"For i = 1 To 10b = InputBox(s1, "猜一猜")If b > a Thens1 = "请输入(大了)"ElseIf b < a Thens1 = "请输入(小了)"ElseMsgBox "恭喜在第" ...
高密市13544742139: 用VB语言编写一个猜数字的游戏?
兴倪帕尼: View.Enabled = False End Sub Private Sub New_Click() 注释:开始一个新游戏时 View.Enabled = True 注释:可以看答案 List1.Clear 注释:清空列表框 Degree = 0 注释: 对随机数生成器做初始化 Randomize Num = 1 Label1.Caption = 0 & ...
高密市13544742139: ■用vb编一个简单的游戏(代码)(要详细) - ?
兴倪帕尼: '猜数字,这个简单了吧. Private Sub Form_Load()Randomizenum = Int(Rnd * 1000)str1 = "输入一个0到999间的整数"Donum1 = Val(InputBox(str1))If num1 > num Thenstr1 = "大了"ElseIf num1 < num Thenstr1 = "小了"ElseMsgBox "恭喜您!答对了."Exit DoEnd IfLoop End Sub
高密市13544742139: 用VB编程实现“看图猜成语”教学设计 - ?
兴倪帕尼: Private Sub Command1_Click() Dim a(4) As String a(1) = "a" a(2) = "b" a(3) = "c" a(4) = "d" For i = 1 To 4 For j = 1 To 4 For x = 1 To 4 For y = 1 To 4 If i <> j And i <> x And i <> y And j <> x And j <> y And x <> y Then Print a(i) & a(j) & a(x) & a(y) Next y Next x Next j Next i End Sub