0

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

2 回答 2

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是全局的,但是您应该将代码更改为 use t1,或者根本不传递该参数。

其次,您在块中有End 0、1 或 2 场比赛。该End语句立即终止执行。,这意味着程序刚刚停止。摆脱那条线。

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

于 2019-03-03T16:01:36.013 回答
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
于 2019-03-04T19:02:50.540 回答