求大神帮忙写个 随机选数和猜数的 VB 程序~ 谢谢!
要求是当用户按newgame(button)的按钮时,程序随机产生一个1-100之间的数字,然后用户可以在guesses(textbox)里输入自己猜的数字,然后当单击e...
要求是当用户按 new game (button)的按钮时, 程序随机产生一个1-100 之间的数字, 然后用户可以在 guesses (textbox) 里输入自己猜的数字, 然后当 单击 enter(button) 时, 如果猜对了, 游戏结束, 用户可以单击new game 开始新游戏 , 如果猜错了, 程序应输出 猜的数字是 高了还是低了。
下面然后还有个要求就是说显示每次的结果的, 我翻译过来我感觉更难理解。。。。, 英语好的大神自己看一下吧…… 跪谢!
Use
instance variables for a Random object and to store the randomly generated number in the range of 1 to 100. If a guess is correct, display “Correct!” in the output Label, then disable the Enter Button and enable the NewGame Button. If a guess is higher than the correct answer, display “Too high”... in the output Label. If a guess is lower than the correct answer, display “Too low”... in the output Label. For the user’s convenience, give the Guess: TextBox the focus after each guess is processed.
我现在有个代码,可是不好用,开头那乱糟糟的,然后中间也有一些我看不懂的, 您能帮忙重新写一个吗?
这个是原来的代码:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Option Explicit
Private Sub Command1_Click(ByVal Index As Integer)
Static nRnd%, nGuessCnt%
Dim strMsg$
Select Case Index
Case 0
nGuessCnt = 0
Randomize()
nRnd = Int(101 * Rnd)
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Debug.Print(nRnd)
Case 1
strMsg = "猜对了"
If (Val(Text1.Text) <> nRnd) Then
If (Val(Text1.Text) < nRnd) Then strMsg = "猜小了,继续加油." Else strMsg = "猜大了,再来一次?"
nGuessCnt = nGuessCnt + 1
End If
MsgBox(strMsg & "我猜了: " & nGuessCnt & " 次", vbInformation, "结果")
If (strMsg = "猜对了") Then Command1_Click(0)
Text1.SetFocus()
SendKeys("{Home}+{End}")
End Select
End Sub
Private Sub Form_Load()
'生成一个随机数.
Command1_Click(0)
End Sub
End Class 展开
下面然后还有个要求就是说显示每次的结果的, 我翻译过来我感觉更难理解。。。。, 英语好的大神自己看一下吧…… 跪谢!
Use
instance variables for a Random object and to store the randomly generated number in the range of 1 to 100. If a guess is correct, display “Correct!” in the output Label, then disable the Enter Button and enable the NewGame Button. If a guess is higher than the correct answer, display “Too high”... in the output Label. If a guess is lower than the correct answer, display “Too low”... in the output Label. For the user’s convenience, give the Guess: TextBox the focus after each guess is processed.
我现在有个代码,可是不好用,开头那乱糟糟的,然后中间也有一些我看不懂的, 您能帮忙重新写一个吗?
这个是原来的代码:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Option Explicit
Private Sub Command1_Click(ByVal Index As Integer)
Static nRnd%, nGuessCnt%
Dim strMsg$
Select Case Index
Case 0
nGuessCnt = 0
Randomize()
nRnd = Int(101 * Rnd)
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Debug.Print(nRnd)
Case 1
strMsg = "猜对了"
If (Val(Text1.Text) <> nRnd) Then
If (Val(Text1.Text) < nRnd) Then strMsg = "猜小了,继续加油." Else strMsg = "猜大了,再来一次?"
nGuessCnt = nGuessCnt + 1
End If
MsgBox(strMsg & "我猜了: " & nGuessCnt & " 次", vbInformation, "结果")
If (strMsg = "猜对了") Then Command1_Click(0)
Text1.SetFocus()
SendKeys("{Home}+{End}")
End Select
End Sub
Private Sub Form_Load()
'生成一个随机数.
Command1_Click(0)
End Sub
End Class 展开
1个回答
展开全部
Dim a As Integer
Private Sub Command1_Click()
Randomize
a = Int(100 * Rnd + 1)
Command1.Enabled = False
End Sub
Private Sub Command2_Click()
If Val(Text1) > a Then MsgBox "猜大了,继续加油."
If Val(Text1) < a Then MsgBox "猜小了,再来一次?"
If Val(Text1) = a Then MsgBox "猜对了,你好厉害!": Command1.Enabled = True
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
Text1.SetFocus
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询