1

ユーザーが入力した整数とコードによって生成された乱数との間で数値比較を実行するという問題に直面しています。

次に、ユーザーが入力した各整数を LOTTO 番号と比較して、一致を確認する必要があります。これを実現するには、For each… ループを使用する必要があります。

7 つのユーザー入力整数すべてを LOTTO 番号と照合した後、一致の総数がユーザーに出力されます。一致するものがない場合、出力は「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

エンドモジュール

4

1 に答える 1

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
于 2012-09-12T20:49:04.123 に答える