vb.net readline或readkey不想停止我的程序

时间:2019-03-03 13:11:27

标签: vb.net console-application readline readkey

我的代码在工作,我尝试过单独尝试,但是这里的问题是,当我将它们放在一起时,readkey或readline不会停止程序,并且do循环也无法工作,有人可以看看吗预先感谢

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

2 个答案:

答案 0 :(得分:1)

您在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

您的参数为t1,但是您在所有代码中都使用temp。如前所述,由于temp是全局的,它仍然可以工作,但是您应该更改代码以使用t1,或者根本不传入该参数。

第二,您在区块中有End,可以进行0、1或2个匹配。 End语句立即终止执行。,这意味着程序刚刚停止。摆脱那条线。

您还有许多其他事情可以更改,但这应该可以解决您的直接问题...

答案 1 :(得分:0)

我将所有显示代码移到了Sub Main。这样,如果要更改平台,可以轻松移动带有业务规则代码的功能。例如,Windows窗体应用程序。然后,您只需更改显示代码即可。

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