如何在给定范围之间生成随机数? VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42129078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate random numbers between given range?VB.NET
提问by uiit
I'm trying to create computer guess gamein which i think number between 1-100and computer generate random number between 1-100and user give response that generated number is too low or too high or correct guess.E.G computer generate number 23if i give input Lthen next time it will generate number between 1 t0 23.If next time if it display 10 then i press Hthen it will generate numbers between 10 and 23 hope you understand what i am trying to do.And same for H.
Thanks
Here is my code i try
我正在尝试创建computer guess game其中我认为数字之间1-100和计算机生成随机数之间的1-100用户给出响应,即生成的数字太低或太高或正确猜测。EG 计算机生成数字,23如果我提供输入,L那么下次它将生成数字在 1 t0 23 之间。如果下一次如果它显示 10 然后我按H然后它会生成 10 到 23 之间的数字希望你明白我想要做什么H。对于. 谢谢这是我尝试的代码
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Declare Variables
Dim strGuess As Char
Dim random As New Random
Dim answer As Integer
Dim low As Integer
Dim high As Integer
Line1:
MessageBox.Show("You pick a number between 0 and 100 and i will try to guess.Respond")
answer = random.Next(1, 100)
strGuess = InputBox("Is it " & answer, "Number Guessing Game")
' lstGuesses.Items.Add(strGuess)'
low = answer
Line2:
If (CChar(strGuess) = CChar("L")) Then
low = random.Next(1, low)
strGuess = InputBox("Is it " & low, "Number Guessing Game")
End If
GoTo Line2
high = answer
Line7:
If (CChar(strGuess) = CChar("H")) Then
high = random.Next(high, 100)
strGuess = InputBox("Is it " & high, "Number Guessing Game ")
End If
GoTo Line7
End Sub
回答by wow_Doge
Try this code,
试试这个代码,
Public Class Form1
Dim strGuess As Char
Dim random As New Random
Dim answer As Integer
Dim low As Integer = 1
Dim high As Integer = 100
Dim Attempt As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("You pick a number between 0 and 100 and i will try to guess.Respond")
answer = random.Next(1, 100)
strGuess = InputBox("Is it " & answer, "Number Guessing Game")
If strGuess = "L" Then
low = answer
LowFunc(answer)
ElseIf strGuess = "H" Then
high = answer
HighFunc(answer)
ElseIf strGuess = "R" Then
MsgBox("I guessed it in " & Attempt & " tries")
ElseIf strGuess = "X" Then
End
End If
End Sub
Private Function LowFunc(ByVal answer As Integer)
Attempt = Attempt + 1
answer = random.Next(low, high)
strGuess = InputBox("Is it " & answer, "Number Guessing Game")
If strGuess = "L" Then
low = answer
LowFunc(answer)
ElseIf strGuess = "H" Then
high = answer
HighFunc(answer)
ElseIf strGuess = "R" Then
MsgBox("I guessed it in " & Attempt & " tries")
ElseIf strGuess = "X" Then
End
End If
Return Nothing
End Function
Private Function HighFunc(ByVal answer As Integer)
Attempt = Attempt + 1
answer = random.Next(low, high)
strGuess = InputBox("Is it " & answer, "Number Guessing Game")
If strGuess = "H" Then
high = answer
HighFunc(answer)
ElseIf strGuess = "L" Then
low = answer
LowFunc(answer)
ElseIf strGuess = "R" Then
MsgBox("I guessed it in " & Attempt & " tries")
ElseIf strGuess = "X" Then
End
End If
Return Nothing
End Function
End Class
--
——
After reviewing this Linkprovided by you, I updated this code. It will keep generating the random number between highand low. The values of highand lowwill be changed accordingly to input Hor Lwhich will be provided by player/user and when the right number will be guessed the player/user should give Ras an input. They can give Xas an input to quit the application. Attemptwill show how many tried it took to guess the correct number.
在查看了您提供的此链接后,我更新了此代码。它将不断在high和之间生成随机数low。的值high和low将被相应地改变,以输入H或L将由播放器/用户提供,并且当右数将被猜到播放器/用户应该给R作为输入。他们可以X作为退出应用程序的输入。Attempt将显示猜出正确数字需要多少次尝试。

