TheMel2203
TheMel2203

Reputation: 13

vb.net readline or readkey don't want to stop my program

my code is working i tried it separately but the problem here is that when i'm putting them together , the readkey or readline don't stop the program and the do loop is not working too, can someone take a look please thank in advance

Dim count As Integer
Dim first(5) As Integer
Dim temp As Integer
Dim answer As String

Sub Main()
    Do
        Console.WriteLine("Please enter your first number")
        first(0) = Console.ReadLine

        Console.WriteLine("Please enter your second number")
        first(1) = Console.ReadLine

        Console.WriteLine("Please enter your third number")
        first(2) = Console.ReadLine

        Console.WriteLine("Please enter your fourth number")
        first(3) = Console.ReadLine
        Console.WriteLine("Please enter your fifth number")
        first(4) = Console.ReadLine

        Console.WriteLine("Please enter your sixth number")
        first(5) = Console.ReadLine

        randomnumber()

        Console.WriteLine("do you want to continue?")
        answer = Console.ReadLine
    Loop Until (answer = "n" Or answer = "No")
    Console.ReadKey()
End Sub

Sub randomnumber()
    Dim r As New List(Of Integer)
    Dim rg As New Random
    Dim rn As Integer
    Dim arraywinner(5) As Integer

    Do
        rn = rg.Next(1, 40)
        If Not r.Contains(rn) Then
            r.Add(rn)
        End If
    Loop Until r.Count = 6

    'store bane random value in array'
    arraywinner(0) = r(0)
    arraywinner(1) = r(1)
    arraywinner(2) = r(2)
    arraywinner(3) = r(3)
    arraywinner(4) = r(4)
    arraywinner(5) = r(5)

    'print random numbers
    count = 0
    While count <= 5
        Console.WriteLine("the randoms numbers are : " & arraywinner(count))
        count = count + 1
    End While

    'look for the amount of number 
    temp = 0
    For count1 As Integer = 0 To 5
        For count2 As Integer = 0 To 5
            If arraywinner(count1) = first(count2) Then
                temp = temp + 1
            End If
        Next
    Next

    If temp = 1 Or temp = 0 Then
        Console.WriteLine("You have got " & temp & " number")
    Else
        Console.WriteLine("You have got " & temp & " numbers")
    End If
    money(temp)
End Sub

Sub money(ByVal t1 As Integer)
    'prend cash'
    If temp = 6 Then
        Console.WriteLine("Jackpot $$$$$$$$$$$$$")
    ElseIf temp = 3 Then
        Console.WriteLine(" money = 120")
    ElseIf temp = 4 Then
        Console.WriteLine("money = 500")
    ElseIf temp = 5 Then
        Console.WriteLine("money= 10,000")
    Else
        Console.WriteLine(" try next time")
        End
    End If
End Sub

Upvotes: 0

Views: 289

Answers (2)

Mary
Mary

Reputation: 15101

I moved all the display code to Sub Main. This way your Functions with your business rules code can easily be moved if you were to change platforms. For example a Windows Forms application. Then all you would have to change is the display code which is all in one place.

Module Module1
Private rg As New Random

Public Sub Main()
    'keep variables with as narrow a scope as possible
    Dim answer As String = Nothing
    'This line initializes and array of strings called words
    Dim words = {"first", "second", "third", "fourth", "fifth", "sixth"}
    Dim WinnersChosen(5) As Integer

    Do
        'To shorten your code use a For loop
        For index = 0 To 5
            Console.WriteLine($"Please enter your {words(index)} number")
            WinnersChosen(index) = CInt(Console.ReadLine)
        Next
        Dim RandomWinners = GetRandomWinners()
        Console.WriteLine("The random winners are:")
        For Each i As Integer In RandomWinners
            Console.WriteLine(i)
        Next
        Dim WinnersCount = FindWinnersCount(RandomWinners, WinnersChosen)

        If WinnersCount = 1 Then
            Console.WriteLine($"You have guessed {WinnersCount} number")
        Else
            Console.WriteLine($"You have guessed {WinnersCount} numbers")
        End If
        Dim Winnings = Money(WinnersCount)
        'The formatting :N0 will add the commas to the number
        Console.WriteLine($"Your winnings are {Winnings:N0}")
        Console.WriteLine("do you want to continue? y/n")
        answer = Console.ReadLine.ToLower
    Loop Until answer = "n"
    Console.ReadKey()
End Sub
'Too much happening in the Sub
'Try to have a Sub or Function do only one job
'Name the Sub accordingly
Private Function GetRandomWinners() As List(Of Integer)
    Dim RandomWinners As New List(Of Integer)
    Dim rn As Integer
    'Good use of .Contains and good logic in Loop Until
    Do
        rn = rg.Next(1, 40)
        If Not RandomWinners.Contains(rn) Then
            RandomWinners.Add(rn)
        End If
    Loop Until RandomWinners.Count = 6
    Return RandomWinners
End Function

Private Function FindWinnersCount(r As List(Of Integer), WinnersChosen() As Integer) As Integer
    Dim temp As Integer
    For count1 As Integer = 0 To 5
        For count2 As Integer = 0 To 5
            If r(count1) = WinnersChosen(count2) Then
                temp = temp + 1
            End If
        Next
    Next
    Return temp
End Function

Private Function Money(Count As Integer) As Integer
    'A Select Case reads a little cleaner
    Select Case Count
        Case 3
            Return 120
        Case 4
            Return 500
        Case 5
            Return 10000
        Case 6
            Return 1000000
        Case Else
            Return 0
    End Select
End Function
End Module

Upvotes: 0

Idle_Mind
Idle_Mind

Reputation: 39152

You have two problems in money():

Sub money(ByVal t1 As Integer)
    'prend cash'
    If temp = 6 Then
        Console.WriteLine("Jackpot $$$$$$$$$$$$$")
    ElseIf temp = 3 Then
        Console.WriteLine(" money = 120")
    ElseIf temp = 4 Then
        Console.WriteLine("money = 500")
    ElseIf temp = 5 Then
        Console.WriteLine("money= 10,000")
    Else
        Console.WriteLine(" try next time")
        End
    End If
End Sub

Your parameter is t1, but you're using temp in all of your code. As written, it will still work since temp is global, but you should either change the code to use t1, or not pass in that parameter at all.

Secondly, you have End in the block for 0, 1, or 2 matches. The End statement Terminates execution immediately., which means the program just stops. Get rid of that line.

There are so many other things you could change, but that should fix your immediate problem...

Upvotes: 1

Related Questions