简体   繁体   English

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

[英]Visual Basic - Looping an array 100 times not working?

I'm trying to loop this array 100 times (gets a number from 0-99,999). 我试图将此数组循环100次(从0-99,999获得一个数字)。 I've tried it several different ways with no luck. 我已经尝试了几种不同的方法,但是没有运气。 I've got it set up so you click a button and it generates the array. 我已经设置好了,所以您单击一个按钮即可生成数组。 The user will then enter a number and hit the other button to check how many times that number shows up in the group of a 100 numbers. 然后,用户将输入一个数字并按下另一个按钮,以检查该数字在100个数字的组中显示了多少次。 Any thoughts how to fix it? 有什么想法如何解决? I'm stuck. 我被卡住了。

   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

Move the New Random() out of that loop. New Random()移出该循环。 Make it global and create the instance just one time. 将其设置为全局并仅创建实例一次。

(The Random generator starts using a seed obtained from the current time. Declaring a Random variable inside a loop result in the same seed used again and again. This gives back the same number defeating the randomness of the function) It is like: http://xkcd.com/221/ (Random生成器开始使用从当前时间获得的种子。在循环内声明Random变量将导致一次又一次使用相同的种子。这将返回相同的数字,从而破坏了函数的随机性) : //xkcd.com/221/

See another explanation here 在这里查看其他说明

Of course, if you don't increment the variable i used to control the loop, you will loop forever 当然,如果你不增加变量i用来控制循环,你将永远循环下去

Dim i As Integer
Dim rnd As New Random()

.....

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

Said that, now I can't get what are you trying to do here. 话说回来,现在我无法理解您要在这里做什么。 There is no array to check and you do not use the value obtained from the Random generator, simply, your i variable exits with value 101. 没有要检查的数组,并且您不使用从Random生成器获得的值,简单来说,您的i变量以值101退出。
Now I am just guessing what are your intentions, but perhaps you want just this? 现在,我只是在猜测您的意图是什么,但是也许您只是想要这个?

i = rnd.Next(0, 100000)

This will give to the variable i a random number and your subsequent code checks how many, of the required digit, are present in i 这将为变量i一个随机数,您的后续代码将检查i中存在多少个所需数字

First of all, it is common to use For Loops when you know the number of loops. 首先,当您知道循环数时,通常使用For循环。

Secondly, you forgot to increment i in the until loop 其次,您忘记在直到循环中增加i

Finally, you don't need to redeclare rnd in every loop. 最后,您不需要在每个循环中都重新声明rnd。 Something like that sounds better to me : 这样的事情对我来说听起来更好:

Dim rnd As New Random()

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

I see that you are not adding the variable "i" to increment: 我看到您没有将变量“ i”添加到增量中:

i = 1
Do Until i > 100
        Dim rnd As New Random()
        Dim randomInt = rnd.Next(0, 100000)
        i+=1 'Increment value of i by 1, same as i = i + 1 
Loop

The variable of "i" will always be 1 if not incremented. 如果不增加,“ i”变量将始终为1。 Check that and let me know if that solves the problem! 检查一下,让我知道是否可以解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM