彩票编码问题

扫码查看
本文介绍了彩票编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一项任务,我正在尝试编写乐透代码。我已经运行了几次,出于某种原因,我的循环只会在我的列表框中添加相同的内容。这是我循环的问题吗?或者是我的编码中的其他内容?

As an assignment, I am trying to write the code for a lottery. I've run it a few times and for some reason, my loop only adds the same thing to my list box. Is it a problem with how I loop it? Or is it something else in my coding?

Private Sub btnTry_Click(sender As Object, e As EventArgs) Handles btnTry.Click
       ' Declare variables.
       Dim intNum1 As Integer
       Dim intNum2 As Integer
       Dim intNum3 As Integer
       Dim intNum4 As Integer
       Dim intNum5 As Integer
       Dim strUserNum1 As String
       Dim strUserNum2 As String
       Dim strUserNum3 As String
       Dim strUserNum4 As String
       Dim strUserNum5 As String
       Dim blnCheck As Boolean = True
       Dim rand1 As New Random
       Dim intCompNum1 As Integer
       Dim intCompNum2 As Integer
       Dim intCompNum3 As Integer
       Dim intCompNum4 As Integer
       Dim intCompNum5 As Integer
       Dim strCompNum As String
       Dim intCount As Integer
       Dim strResult As String

       ' Convert the user's numbers and error check for repetition.
       Integer.TryParse(txtNum1.Text, intNum1)
       Integer.TryParse(txtNum2.Text, intNum2)
       Integer.TryParse(txtNum3.Text, intNum3)
       Integer.TryParse(txtNum4.Text, intNum4)
       Integer.TryParse(txtNum5.Text, intNum5)
       If intNum1 = intNum2 Or intNum2 = intNum3 Or intNum3 = intNum4 Or
           intNum4 = intNum5 Then
           lblStatus.Text = "Remember to not enter the same number twice."
           blnCheck = False
       End If

       ' Have the computer generate random numbers.
       intCompNum1 = rand1.Next(60) + 1
       intCompNum2 = rand1.Next(60) + 1
       intCompNum3 = rand1.Next(60) + 1
       intCompNum4 = rand1.Next(60) + 1
       intCompNum5 = rand1.Next(60) + 1

       ' Check to ensure random numbers are not the same.
       Do
           If Not intCompNum1 = intCompNum2 And intCompNum2 = intCompNum3 And
               intCompNum3 = intCompNum4 And intCompNum4 = intCompNum5 Then
               blnCheck = True
           Else
               blnCheck = False
               intCompNum1 = rand1.Next(60) + 1
               intCompNum2 = rand1.Next(60) + 1
               intCompNum3 = rand1.Next(60) + 1
               intCompNum4 = rand1.Next(60) + 1
               intCompNum5 = rand1.Next(60) + 1
           End If
       Loop Until blnCheck = True

       ' Start building the output string and check for matching numbers.
       For intCount = 1 To 10000
           strUserNum1 = CStr(intNum1)
           strUserNum2 = CStr(intNum2)
           strUserNum3 = CStr(intNum3)
           strUserNum4 = CStr(intNum4)
           strUserNum5 = CStr(intNum5)
           strCompNum = intCompNum1.ToString() & ", " & intCompNum2.ToString() &
               ", " & intCompNum3.ToString() & ", " & intCompNum4.ToString() &
               ", " & intCompNum5.ToString()
           strResult = "Attempt " & intCount.ToString() & ": You matched "
           If blnCheck = True Then
               If strCompNum.IndexOf(strUserNum1) <> -1 Then
                   strResult &= intNum1.ToString() & ", "
               End If

               If strCompNum.IndexOf(strUserNum2) <> -1 Then
                   strResult &= intNum2.ToString() & ", "
               End If

               If strCompNum.IndexOf(strUserNum3) <> -1 Then
                   strResult &= intNum3.ToString() & ", "
               End If

               If strCompNum.IndexOf(strUserNum4) <> -1 Then
                   strResult &= intNum4.ToString() & ", "
               End If

               If strCompNum.IndexOf(strUserNum5) <> -1 Then
                   strResult &= intNum5.ToString
               End If

               ' Add the input to the lstOutput box.
               lstOutput.Items.Add(strResult)
           End If
       Next
   End Sub

推荐答案

Dim balls As List(Of Integer) = Enumerable.Range(1, 49).ToList()



然后从列表中生成六个随机数:


Then generate six random numbers from the list:

Dim balls As List(Of Integer) = Enumerable.Range(1, 49).ToList()
Dim winner As New List(Of Integer)()
For i As Integer = 0 To 5
    Dim winBall As Integer = Random.[Next](0, balls.Count)
    winner.Add(balls(winBall))
    balls.RemoveAt(winBall)
Next

你可以使用所有球的列表,并删除每个球的列表选中,保证不会重复。



那么你所要做的就是对用户输入的数字进行排序,对获胜的球数进行排序,你可以比较非常简单!



有意义吗?

Becaue you use a list of all balls, and remove each one when it's selected, you are guaranteed not to get any repetitions.

Then all you have to do is sort the user input numbers, sort the winning ball numbers, and you can compare them very simply!

Make sense?


这篇关于彩票编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:49
查看更多