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

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

我的代码可以正常工作 我单独尝试过,但这里的问题是当我将它们放在一起时,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

您在 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语句立即终止执行。,这意味着程序只是停止。摆脱那条线。

您可以更改许多其他内容,但这应该可以解决您眼前的问题...

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

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