Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   only displays 1 number not 6 (http://www.programmingforums.org/showthread.php?t=13445)

boatn19 Jun 29th, 2007 2:30 PM

only displays 1 number not 6
 
the following code is only displaying one number and I am trying to get it display six numbers
:

        Dim number(5) As Integer
        Dim subscript As Integer
        Dim searchSubscript As Integer
        Dim isFound As Boolean
        Dim raNumber As Integer
        Dim randomGenerator As New Random

        number(0) = 5
        subscript = number(1)
        Do While subscript < number.Length
            raNumber = randomGenerator.Next(1, 54)
            searchSubscript = 0
            Do While searchSubscript < subscript
                isFound = False
                If searchSubscript = raNumber Then
                    isFound = True
                Else
                    searchSubscript = searchSubscript + 1
                End If
            Loop
            If isFound = False Then
                subscript = raNumber
                subscript = subscript + 1
            End If
        Loop
        'display the numbers
        Me.xLotteryLabel.Text = (subscript.ToString())
    End Sub
End Class


mackenga Jul 10th, 2007 6:53 PM

I can see what you're trying to do there, but you're getting confused between numbers, subscripts, etc. You start off by generating numbers into your array, then you display the numbers (right?). So how about:

:

Dim I As Integer, J As Integer
Dim intNumber(5) As Integer
Dim intNew As Integer
Dim rndGen As New Random

For I = 0 To 5
    intNew = rndGen.Next(1, 54)
    For J = 0 To I - 1
        If intNumber(J) = intNew Then
            I = I - 1
            Exit For
        End If
    Next J
Next I

For I = 0 To 5
    lblResult.Caption = lblResult.Caption & intNumber(I)
Next


Notice I don't really need the boolean to note whether I've found the number already there; instead, after the new number is generated I search through the array to see if it's already there and take one off I if it is, which will push the outer loop back so it runs this step again next time. Then I Exit For to duck out of the inner For loop to let that happen.

This is probably not the best way and isn't exactly what you were asking for but I was finding your code confusing, so I hope you can figure out how to put the two together and that this helps. I also hope it runs - I don't actually have VB handy to try it out!

zaracyn Aug 3rd, 2007 1:21 PM

You both appear to be forgetting to populate your array, it might look something like this:


Private Sub FindWinningLottoNumberButton_Click()
Dim I As Integer
Dim lottoNum As Integer
Dim lottoNumArray(5) As Integer

For I = 0 To 5
lottoNum = Rand(1, 56)
lottoNumArray(I) = lottoNum
' place code to check for unique numbers here
Next I

For I = 0 To 5
TextBox1.Text = TextBox1.Text & lottoNumArray(I) & " "
Next I

TextBox1.Text = TextBox1.Text & vbNewLine
End Sub


Hope this helps!

Jabo Sep 3rd, 2007 10:47 PM

Quote:

Originally Posted by boatn19 (Post 129824)
:

number(0) = 5
subscript = number(1)


This looks to me like a data error.
number(0) is initialized to value 5, number(1) will be initialized to 0, unless that's what you were counting on.


All times are GMT -5. The time now is 11:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC