简体   繁体   中英

Having trouble with a lottery software

sorry to come in here like this looking for answers, but im actually really stumped. Im supposed to make an application using 2 forms, one to generate its own array and store user entered numbers into another array, and then display them on the second form and tell you how many of the numbers are in common

I cant get the numbers to show up on the second form at all, and I also cant really understand how to get how many numbers are matching, but I will when I can reference them properly in form 2. I got the arrays set up perfectly, but I cant even call the numbers from them to form 2. I need help calling the arrays from the first form to display on the next

Form 1 code

Public Class Form1
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub
Shared random As New Random()
Dim UserArray(4) As String
Dim LotteryArray(4) As String
Public Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
    If txtNumbers.Text <> "" Then
        For i As Integer = 0 To 4
            If UserArray(i) = "" Then
                UserArray(i) = txtNumbers.Text
            End If
        Next
    End If

    For n As Integer = 0 To 9
        Dim RandomNumber As Integer = CInt(Int(Rnd() * 5) + 1)
        LotteryArray(4) = CStr(RandomNumber)
    Next
End Sub
End Class

Form 2 Code

    Public Class Form2
    
    Public Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
    lblUser1.Text = Form1.UserArray(0)
    lblUser2.Text = Form1.UserArray(1)
    lblUser3.Text = Form1.UserArray(2)
    lblUser4.Text = Form1.UserArray(3)
    lblUser5.Text = Form1.UserArray(4)

    lblRand1.Text = Form1.LotteryArray(0)
    lblRand2.Text = Form1.LotteryArray(1)
    lblRand3.Text = Form1.LotteryArray(2)
    lblRand4.Text = Form1.LotteryArray(3)
    lblRand5.Text = Form1.LotteryArray(4)
    If Form1.LotteryArray(4) = Form1.UserArray(4) Then
        MessageBox("CONGRATULATIONS!", "You Are The GRAND PRIZE WINNER!")
    End If
    Me.Close()
End Sub
End Class

I will address your first question. How to get your numbers into your second form.

You main problem is the Access modifier you used for your arrays. Dim is the equivalent of Private which means that the variable is only visible within the class. Friend is visible anywhere in the assembly. See https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/access-levels for more information. Don't change your event procedure Access to Public.

You have to let the user fill in another guess. The first loop in the Check button will only enter the same input 5 times. You need one entry on every click but you need to keep track of where you are in the array. Private NumberOfGuesses As Integer Note that this variable is Private . It doesn't need to be seen outside of the Form1 class.

We don't need to refill the LotteryArray on every click so I did it once in the Form.Load You were trying to use the Random class the way you use the old vb6 Random. The .net class is much easier.

To display the arrays on Form2, I used list boxes and loops. Saved a bit of typing.

If you still have a question about part 2 of your problem post another question. Show what you have tried.

Public Class Form1
    Private random As New Random()
    Friend UserArray(4) As Integer
    Friend LotteryArray(9) As Integer
    Private NumberOfGuesses As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For n As Integer = 0 To 9
            Dim RandomNumber As Integer = random.Next(0, 10)
            LotteryArray(n) = RandomNumber
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If NumberOfGuesses > 4 Then
            MessageBox.Show("You have used up your guesses")
            Form2.Show()
            Me.Close()
        End If
        If TextBox1.Text <> "" Then
            UserArray(NumberOfGuesses) = CInt(TextBox1.Text)
            NumberOfGuesses += 1
            TextBox1.Clear()
            TextBox1.Focus()
        End If
    End Sub

End Class

Public Class Form2

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each number In Form1.UserArray
            ListBox1.Items.Add(number)
        Next
        For Each number In Form1.LotteryArray
            ListBox2.Items.Add(number)
        Next
    End Sub

End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM