gpt4 book ai didi

vb.net - Visual Basic - 循环数组 100 次不起作用?

转载 作者:行者123 更新时间:2023-12-02 05:09:51 25 4
gpt4 key购买 nike

我正在尝试将此数组循环 100 次(获取 0-99,999 之间的数字)。我已经尝试了几种不同的方法但没有运气。我已经设置好了,所以你点击一个按钮,它就会生成数组。然后用户将输入一个数字并点击另一个按钮以检查该数字在 100 个数字的组中出现了多少次。任何想法如何解决它?我卡住了。

   Dim i As Integer
Dim rnd As New Random()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click


Dim num As Integer
If Int32.TryParse(txtEnter.Text, num) AndAlso num >= 0 AndAlso num < 10 Then
lblNumber.Text = i.ToString()
Dim counts = From c In i.ToString()
Group By c Into Group
Select DigitGroup = New With {.Count = Group.Count(), .Digit = c, .Group = Group}
Order By DigitGroup.Count Descending, DigitGroup.Digit
Dim numCount = counts.FirstOrDefault(Function(grp) grp.Digit.ToString() = num.ToString())
If numCount IsNot Nothing Then
Dim numMessage = String.Format("There are {0} number {1}'s found.", numCount.Count, num)
MessageBox.Show(numMessage)
Else
MessageBox.Show("Your number does not contain a " & num)
End If
Else
MessageBox.Show("Please enter a number between 0 and 9.")
End If

End Sub

Private Sub btnGetNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetNumber.Click
btnEnter.Enabled = True


i = 0
Do Until i > 100
Dim randomInt = rnd.Next(0, 100000)
i = i + 1
i = rnd.Next(0, 100000)
Loop
End Sub
End Class

最佳答案

首先,当您知道循环次数时,通常会使用 For 循环。

其次,你忘了在 until 循环中递增 i

最后,您不需要在每个循环中都重新声明 rnd。像这样的东西对我来说听起来更好:

Dim rnd As New Random()

For i = 1 To 100
Dim randomInt = rnd.Next( 0 , 100000 )
Next

关于vb.net - Visual Basic - 循环数组 100 次不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15671593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com