Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Having issues with me visual basic lottery program. The program is suppose to ta

ID: 640614 • Letter: H

Question

Having issues with me visual basic lottery program. The program is suppose to take the 5 numbers from the user between the values 1-9. If all 5 numbers are correct then user is a winner. This is what I have so far.

Public Class Form1

'The array for holding lottery number
Dim lotteryNumber(4) As Integer

Private Sub btncompareLotteryNumber_Click(sender As Object, e As EventArgs) Handles btnLuck.Click
Dim rand = New Random()
For i = 0 To 4
lotteryNumber.SetValue(rand.Next(10), i)
Next

Dim input = txtLotNum1.Text().ToCharArray()
Dim input = txtLotNum2.Text().ToCharArray()
Dim input = txtLotNum3.Text().ToCharArray()
Dim input = txtLotNum4.Text().ToCharArray()
Dim input = txtLotNum5.Text().ToCharArray()

Dim matchedDigits = 0

For i = 0 To lotteryNumber.Length - 1
Dim temp = Convert.ToInt32(input.ElementAt(i))
temp = temp - Convert.ToInt32("0"c)
If temp = lotteryNumber.ElementAt(i) Then
matchedDigits += 1
End If
Next

If matchedDigits = 5 Then
MsgBoxResult. = "Congratulations! You won the lottery!"
ElseIf matchedDigits < 5 Then
MsgBoxResult. = Convert.ToString(matchedDigits) + " digits matched"
End If
End Sub
End Class

Explanation / Answer

The code is below. It's a bit awkward, but it does the job.

I have used 5 labels to display the random number groups.

Label 1 for the 3 numbers (0 to 9); Label 2 for the 4 numbers (0 to 9); Label 3 for the 5 numbers (1 to 39); Label 4 for the 5 numbers (1 to 39) and Label5 for the 1 number(1 to 42).

You can use the generated numbers anywhere you wish.




Public Class Form1
Public Nums3, Nums4, Nums5, Nums51, Nums511 As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Nums3 = Lotto3()
Label1.Text = Nums3
Nums4 = Lotto4()
Label2.Text = Nums4
Nums5 = Lotto5()
Label3.Text = Nums5
Nums51 = Lotto51()
Label4.Text = Nums51
Nums511 = Lotto511()
Label5.Text = Nums511



End Sub
Function Lotto3()
Dim Num1, Cnt, Flag1 As Integer

Dim Pack(9) As Integer
Randomize()
Do
Randomize()
If Cnt = 0 Then
Num1 = Fix(Rnd() * 10)
Else
Num1 = Fix(Rnd() * 10)
For j = 0 To Cnt - 1
If Num1 = Pack(j) Then
Flag1 = 1
End If
Next
End If
If Flag1 = 1 Then
Flag1 = 0
Else
Pack(Cnt) = Num1
Nums3 = Nums3 & Num1 & " "
Cnt += 1
End If
Loop Until Cnt = 3
Return (Nums3)
End Function

Function Lotto4()
Dim Num1, Cnt, Flag1 As Integer

Dim Pack(9) As Integer
Randomize()
Do
Randomize()
If Cnt = 0 Then
Num1 = Fix(Rnd() * 10)
Else
Num1 = Fix(Rnd() * 10)
For j = 0 To Cnt - 1
If Num1 = Pack(j) Then
Flag1 = 1
End If
Next
End If
If Flag1 = 1 Then
Flag1 = 0
Else
Pack(Cnt) = Num1
Nums4 = Nums4 & Num1 & " "
Cnt += 1
End If
Loop Until Cnt = 4
Return (Nums4)
End Function

Function Lotto5()
Dim Num1, Cnt, Flag1 As Integer
Cnt = 1
Dim Pack(39) As Integer
Randomize()
Do
Randomize()
If Cnt = 1 Then
Num1 = Fix(Rnd() * 39) + 1
Else
Num1 = Fix(Rnd() * 39) + 1
For j = 0 To Cnt - 1
If Num1 = Pack(j) Then
Flag1 = 1
End If
Next
End If
If Flag1 = 1 Then
Flag1 = 0
Else
Pack(Cnt) = Num1
Nums5 = Nums5 & Num1 & " "
Cnt += 1
End If
Loop Until Cnt = 6
Return (Nums5)
End Function

Function Lotto51()
Dim Num1, Cnt, Flag1 As Integer
Cnt = 1
Dim Pack(39) As Integer
Randomize()
Do
Randomize()
If Cnt = 1 Then
Num1 = Fix(Rnd() * 39) + 1
Else
Num1 = Fix(Rnd() * 39) + 1
For j = 0 To Cnt - 1
If Num1 = Pack(j) Then
Flag1 = 1
End If
Next
End If
If Flag1 = 1 Then
Flag1 = 0
Else
Pack(Cnt) = Num1
Nums51 = Nums51 & Num1 & " "
Cnt += 1
End If
Loop Until Cnt = 6
Return (Nums51)


End Function

Function Lotto511()
Randomize()
Nums511 = Int(Rnd() * 42) + 1
Return (Nums511)
End Function
End Class

Thank You
Please Rate

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote