Vb.net 我很难理解嵌套循环中的逻辑

Vb.net 我很难理解嵌套循环中的逻辑,vb.net,loops,nested,Vb.net,Loops,Nested,我正在写一个应用程序,分析以前的彩票强力球抽奖,以增加中奖几率。以下是我迄今为止所做的工作: 我从这样一个文件中带来了完整的图纸: 06/29/2002 01 02 03 04 05强力球:01 我删掉了这些数字,这样我就可以在它们之间循环,用数字和数字的绘制次数填充一个数组。 我想获取任何特定数字被绘制的次数,以及数字,即数字1和44被绘制108次。我想将数据存储在这样的频率网格中,1,44=108(数字1和44被绘制了108次)。以下是迄今为止我对此的代码: Private Class Ge

我正在写一个应用程序,分析以前的彩票强力球抽奖,以增加中奖几率。以下是我迄今为止所做的工作:

我从这样一个文件中带来了完整的图纸: 06/29/2002 01 02 03 04 05强力球:01 我删掉了这些数字,这样我就可以在它们之间循环,用数字和数字的绘制次数填充一个数组。 我想获取任何特定数字被绘制的次数,以及数字,即数字1和44被绘制108次。我想将数据存储在这样的频率网格中,1,44=108(数字1和44被绘制了108次)。以下是迄今为止我对此的代码:

Private Class GetNumberFrequency
    Public ReadOnly Property GetFrequencyGrid() As String
        Get
           'Get/Set Variables
            Dim Size As Integer = CInt((Globals.UsersNumbers.Length /3))
            Dim TempNumber As Integer = 0
            Dim Frequency(59) As Integer   
            Dim Temp1 As Integer = 0
            Dim SortedFrequency1(59) As Integer
            Dim SortedFrequency2(59) As Integer
            Dim Start As Integer = 0

             For x As Integer = 0 To Size - 1 Step 1
                TempNumber = CInt(Globals.UsersNumbers.Substring(Start, 3).TrimStart)
                Frequency(TempNumber) += 1
                Start += 3
            Next

            For i As Integer = 1 To Frequency.Length - 1 Step 1
                Temp1 = Frequency(i) 'Get a number

                For j As Integer = 1 To Frequency.Length - 1 Step 1
                    If Frequency(j) = Temp1 Then
'Here is where I am having the problem.
'I cant figure out the logic to use here.
'Right now the array holds the numbers and the number of times they
'were drawn,
'i.e. Frequency(1) = 108
      Frequency(2) = 117
      Frequency(3) = 106
      Frequency(44) = 108
'I want to loop through the array values 108, 117, 106 and grab 
'the indexes of each of these number draw frequencies (1, 2, 3, etc.), 
'so I can display them as,
    'Numbers    Frequency
    '1, 44        108
    '7, 25        117, etc. 
'I've tried using a 2 dimensional array but the array of Frequency(60, 60)
'creates an array of 3600 elements, and I don't need that many.
'I've also tried an array for the numbers and a string for the number of
'times drawn, but the logic escapes me.
'Any help will be appreciated! Thank you.
                    End If
                Next
            Next
            Return Frequency.ToString
        End Get
    End Property
End Class

通用列表和字典是存储和组织信息(而不是数组)的好方法。为此,我将首先生成一些随机数据,因为我没有您正在使用的数据集

A将数据存储为键值对,并且可以为几乎任何可能的类型构造数据。键是唯一的,每个键链接到一个值

在本例中,我想首先将原始数据存储在一个字典中,该字典的键将是
Integer
类型,并且将表示球数。字典的值也将是
Integer
类型,并将保持球历史上绘制的频率

    ' store frequencies in a dictionary (ball number, draw count)
    Dim Frequencies As New Dictionary(Of Integer, Integer)
    Dim r As New Random()
    'generate some random frequency values
    Dim i As Integer
    For i = 1 To 59  'ball numbers 1->59
        Frequencies.Add(i, r.Next(10))
    Next
接下来,我们要根据球的抽签频率对球进行分组。在这里,我再次使用字典,但这次键是一个表示频率计数的
整数。字典在这里同样有用,因为它的目标是使频率计数唯一,并将其与以该频率绘制的球列表相关联。因此,字典的值部分是一个球数列表(整数)
列表(整数)

然后,要生成输出,只需在字典上枚举即可。字典不能排序,但我们可以提取键并对它们进行排序,然后使用已排序的键列表为字典编制索引

    ' extract all the keys (frequencies) and sort them
    Dim f As New List(Of Integer)
    For Each k As Integer In BallsByFreq.Keys
        f.Add(k)
    Next
    f.Sort()
    f.Reverse() 'want highest first

    For Each freq As Integer In f
        Dim outline As String
        outline = "Frequency : " & freq.ToString("000") & ", Balls : "
        'Sort ball numbers to be pretty
        BallsByFreq(freq).Sort()
        For Each ball As Integer In BallsByFreq(freq)
            outline = outline & ball.ToString("00") & " "
        Next
        Console.WriteLine(outline)
    Next
在控制台应用程序中,上述命令生成输出

频率:009,球数:192123253445
频率:008,球数:01 06 31 50 58
频率:007,球数:1140557
频率:006,球数:021417293946
频率:005,球数:0524384144
频率:004,球数:03 07 10 12 22 48 54
频率:003,球数:0918364756
频率:002,球数:04 15 26 32 42 43 49 52 55 59
频率:001,球数:08 16 20 33 35
频率:000,球数:132728307


你可以去powerball的网站。。。虽然这可能会为您提供一种错误的数字选择方法,但不幸的是,这不会增加您实际获胜的几率。添加
步骤1
是多余的,因为这是默认步骤。谢谢您。。。我现在正在编写代码。既然我是vb的初学者,我会让你知道它是怎么回事。net我如何添加关于这个答案的另一个问题?@JimConace我认为这已经足够了。如果你有一个新问题,那么最好的方法就是发布一个新问题。您原来的问题非常广泛,因此,一个新的、更具体的问题涉及到这个答案的内容,可能是一个更好的问题,而且更符合主题。@JimConace请记住,堆栈溢出的目的不一定是在一个地方为您的项目提供解决方案,而是生成一个小的,对更广泛的受众具有持久价值的广泛有用的问题和答案。密集的QA解决您的特定问题可能不会有持久的价值,但您需要回答的许多问题可能会有持久的价值。试着把你的项目分成几个小步骤,在你遇到困难的地方分别提问。范围越具体越窄,答案就越有可能对其他人有用。。。我会的。再次感谢你的耐心,哈哈
    ' extract all the keys (frequencies) and sort them
    Dim f As New List(Of Integer)
    For Each k As Integer In BallsByFreq.Keys
        f.Add(k)
    Next
    f.Sort()
    f.Reverse() 'want highest first

    For Each freq As Integer In f
        Dim outline As String
        outline = "Frequency : " & freq.ToString("000") & ", Balls : "
        'Sort ball numbers to be pretty
        BallsByFreq(freq).Sort()
        For Each ball As Integer In BallsByFreq(freq)
            outline = outline & ball.ToString("00") & " "
        Next
        Console.WriteLine(outline)
    Next