VB 2008(控制台)“For Each Loop”彩票结果比较

时间:2012-09-12 18:16:54

标签: vb.net

我面临一个问题,即在用户输入的整数和代码生成的随机数之间进行数字比较。

然后应将用户输入的每个整数与LOTTO数字进行比较以检查匹配。 A需要使用每个...循环来实现此目的。

在根据LOTTO号码检查所有7个用户输入整数后,应将总匹配数输出给用户。如果没有匹配,则输出应为“LOOSER!”。

这是我的代码,我目前只停留在比较部分,我们需要使用每个循环来实现这一点。

导入系统

模块乐透

Sub Main()

    'Declaration
    Dim numbers(6) As Integer
    Dim IsStarted As Boolean = True

    'Prompt user to enter 
    Console.WriteLine("Please enter your 7 lucky numbers from 0 - 9 ONLY")

    'Use Do While Loop to re-iterate the prompts
    Do While IsStarted
        For pos As Integer = 0 To 6
            Console.Write("Enter number {0}: ", pos + 1)
            'How it stores into an array
            numbers(pos) = Console.ReadLine()

            'Check if it is a number: use IsNumberic()
            If IsNumeric(numbers(pos)) Then 'proceed
                'Check if it is NOT 0 < x > 9
                If numbers(pos) < 0 Or numbers(pos) > 9 Then
                    'Don't proceed
                    Console.WriteLine("Invalid Input")
                    IsStarted = True
                    'When any number is invalid, exit the loop
                    Exit For
                End If
            End If
            IsStarted = False
        Next
    Loop

    'Printing out the array. It can also be written as 
    'For pos = LBound(numbers) To UBound(numbers)
    For pos = 0 To 6
        Console.Write(numbers(pos) & " ")
    Next

    Console.WriteLine()
    'Random number generator
    Randomize()
    Dim random_numbers(6) As Integer
    Dim upperbound As Integer = 7
    Dim lowerbound As Integer = 0
    Dim rnd_number As Double = 0

    For pos = 0 To 6
        rnd_number = CInt((upperbound - lowerbound) * Rnd() + lowerbound)
        random_numbers(pos) = rnd_number

        Console.Write(random_numbers(pos) & " ")
    Next

    'Iterate and compare
    Dim isSame As Boolean = False
    Dim pos2 As Integer = 0
    Dim Counter As Integer = 0

    'For check = 0 To 6
    'If numbers(pos2).Equals(random_numbers(pos2)) Then
    For Each number As Integer In numbers
        'Console.WriteLine(pos2 + 1 & ":" & number & ":")
        If number.Equals(random_numbers(pos2)) Then
            'Console.WriteLine("here is the number that matched:" & number & ":")
            isSame = True
            pos2 = pos2 + 1
        End If

        For Each num As Integer In random_numbers
            If random_numbers Is numbers Then
                Counter = Counter + 1
            End If

        Next
    Next

    Console.WriteLine()

    'Display result
    If isSame = True Then
        Console.WriteLine("The total numbers of matches are: " & Counter)
    Else
        Console.WriteLine("LOOSER!")
    End If
    Console.ReadLine()

End Sub

结束模块

1 个答案:

答案 0 :(得分:0)

            Dim intCursor As Integer = 0

            For Each intNumber As Integer In numbers

                'Assumes first user chosen number must equal 
                'the first random number to be considered a match,
                'the second user number must equal the second random,
                'etc (Ordering is a factor).
                If random_numbers(intCursor) = intNumber Then

                    Counter += 1

                End If

                intCursor += 1

            Next

            If (Counter > 0) Then

                Console.WriteLine("The total numbers of matches are: " & Counter)

            Else

                Console.WriteLine("LOOSER!")

            End If