Question For Loop to search and determain how many their are.

Phil1987

Member
Joined
Nov 18, 2010
Messages
9
Programming Experience
Beginner
Hello, I've just started getting into programming and I'm following a few tasks that have been set to learn and understand VB.NET. What i'm working on is a lottery program i've done all the number generation with an array. Now i'm trying to find out how to search that array for lets say less than or equal to 2 you've lost sadly its not working.

Bellow is the code and every time even without anything in the Textbox it doesn't work and just displays you've won the £1,000,000.

ballbox is an array that contains 5 numbers that are generated by another button.

VB.NET:
    Private Sub BTN_Check_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Check.Click
        Dim i As Integer = 0
        Dim Textbox1 As Integer = 0
        Dim found As Integer = 0

        i = Textbox1

        For Each i In ballbox
            found += 1
        Next

        If found <= 2 Then
            MsgBox("You have lost")
        ElseIf found = 3 Then
            MsgBox("You have won £10")
        ElseIf found = 4 Then
            MsgBox("You have won £450")
        ElseIf found = 5 Then
            MsgBox("You have won £5,000")
        ElseIf found = 6 Then
            MsgBox("You have won £1,000,000")
        End If
 
In your for loop you're just adding 1 to found for every integer in ballbox and not checking it against anything.

How would I go about checking exactly what's in the array? As I might have to change the way I add to the array because a textbox contains the past draws and might cause errors and I would like to check with these past draws as well. Would it be possible to give an example of this?
 
Last edited:
Small example here.

I'm not sure how you're having the user select numbers for their 'card'. In this case I just hardcoded 10 numbers in and split them on the ','. This would be bad UX and you'll want a more elegant way of letting the user select their numbers.

The important part of the code is the For loop which iterates through each number the user selected and Array.Contains which checks the 'ballbox' to see if there is a match.

VB.NET:
        'Create a random enumable range between 1 and 100 and take 5
        Dim rnd As New Random
        Dim nums = Enumerable.Range(1, 100) _
                   .OrderBy(Function() rnd.Next) _
                   .Take(5) _
                   .ToArray()

        'Hardcoded numbers the person selected for testing
        TextBox1.Text = "1,11,22,33,44,55,66,77,88,99"
        Dim selectedNumbers = TextBox1.Text.Split(",")

        Dim match As Integer = 0

        'Loop to cycle through each number the user selected
        For Each num In selectedNumbers
            If nums.Contains(num) Then
                'If the "ballbox" contains the number add 1 to your match counter
                match += 1
            End If
        Next

        MessageBox.Show("Number of matches = " & match.ToString)
    End Sub

I don't think there are any errors here but intellisense tanked on me and I can't test it until after the repair process is done.
 
Ok, that's not exactly what I'm trying to learn I'm trying to learn how to use a loop within an array. I'm doing this by having a textbox which you enter the numbers like "1 2 3 4 5 6" then you press a button which searches the arraylist for a match.

I know I'm very close to understanding this now as I at least get a loss but i can't search for more than 1 number without a crash.

PS: storage is a global arraylist which stores the past generated numbers.

VB.NET:
        Dim temp As Integer
        temp = TextBox1.Text
        Dim found As Integer = 0

        For i = 0 To 5
            If temp = storage(i) Then
                found += 1
            End If
        Next

        If found <= 2 Then
            MsgBox("You have lost")
        ElseIf found = 3 Then
            MsgBox("You have won £10")
        ElseIf found = 4 Then
            MsgBox("You have won £450")
        ElseIf found = 5 Then
            MsgBox("You have won £5,000")
        ElseIf found = 6 Then
            MsgBox("You have won £1,000,000")
        End If
 

Latest posts

Back
Top