Vb.net 问题异常与随机数

Vb.net 问题异常与随机数,vb.net,exception,random,Vb.net,Exception,Random,我在VB.NET中彩票 我模拟这个东西时出错了。要在文件中写入任何“坯料”,生成的错误为: 未处理无效的CastException:“Double”中的链“[”的转换无效 另外,我不知道如何为一个坯料对象的每个数字生成一系列介于1到49之间的随机数 这是我的密码 Public Class Billet Dim _num1, _num2, _num3, _num4, _num5, _num6 As Integer Dim rand As Random Sub New(ByVal _num1 A

我在VB.NET中彩票

我模拟这个东西时出错了。要在文件中写入任何“坯料”,生成的错误为:

未处理无效的CastException:“Double”中的链“[”的转换无效

另外,我不知道如何为一个坯料对象的每个数字生成一系列介于1到49之间的随机数

这是我的密码

Public Class Billet

Dim _num1, _num2, _num3, _num4, _num5, _num6 As Integer
Dim rand As Random

Sub New(ByVal _num1 As Integer, ByVal _num2 As Integer, ByVal _num3 As Integer, ByVal _num4 As Integer, ByVal _num5 As Integer, ByVal _num6 As Integer)
    Me.Num1 = _num1
    Me.Num2 = _num2
    Me.Num3 = _num3
    Me.Num4 = _num4
    Me.Num5 = _num5
    Me.Num6 = _num6
End Sub

Public Property Num1() As Integer
    Get
        Return _num1
    End Get

    Set(ByVal Value As Integer)
        _num1 = Value
    End Set
End Property

Public Property Num2() As Integer
    Get
        Return Num2
    End Get

    Set(ByVal Value As Integer)
        _num2 = Value
    End Set
End Property

Public Property Num3() As Integer
    Get
        Return _num3
    End Get

    Set(ByVal Value As Integer)
        _num3 = Value
    End Set
End Property

Public Property Num4() As Integer
    Get
        Return Num4
    End Get

    Set(ByVal Value As Integer)
        _num4 = Value
    End Set
End Property

Public Property Num5() As Integer
    Get
        Return _num5
    End Get
    Set(ByVal value As Integer)

    End Set
End Property

Public Property Num6() As Integer
    Get
        Return _num6
    End Get
    Set(ByVal value As Integer)

    End Set
End Property

Public Overrides Function ToString() As String
    Return "[" + Num1 + "]"   <----- ERROR :Invalid CastException : La conversion de la chaîne "[" en type 'Double' n'est pas valide.
End Function

End Class

你可以吃类似的东西

Dim rnd As New Random()
Dim b As New Billet(rnd.Next(49), rnd.Next(49), rnd.Next(49), rnd.Next(49), rnd.Next(49), rnd.Next(49))
创建每个方坯对象。

您应该使用符号(
&
)进行字符串连接。双精度上的
+
运算符尝试将字符串转换为双精度,而不是按预期将双精度转换为字符串。因此,它正在尝试添加
“[”
(这显然不是一个数字)到
Num1
。因此出现
InvalidCastException

您的代码应为:

Return "[" & Num1 & "]"
普通彩票

Private Sub Button1_Click(sender As System.Object, _
                          e As System.EventArgs) Handles Button1.Click

    Dim myLotto As New Lottery(49) 'fill the hopper
    myLotto.Draw(6) 'draw the balls
    Debug.WriteLine(myLotto.ToString) 'get the results

    'OR get the results one item at a time
    For x As Integer = 0 To myLotto.Count - 1
        Debug.WriteLine(myLotto.Item(x))
    Next
End Sub

Class Lottery
    Private Shared PRNG As New Random
    Private _theHopper As List(Of Integer)
    Private _draw As List(Of Integer)

    Public Sub New(maxNumber As Integer)
        Me.MaxNumber = maxNumber
        Me._theHopper = New List(Of Integer)
        Me._theHopper.AddRange(Enumerable.Range(1, Me.MaxNumber).ToArray)
    End Sub

    Private _maxNumber As Integer
    Public Property MaxNumber() As Integer
        Get
            Return Me._maxNumber
        End Get
        Set(ByVal value As Integer)
            Me._maxNumber = value
        End Set
    End Property

    Public Sub Draw(numberOfBalls As Integer, _
                    Optional DuplicatesAllowed As Boolean = False)
        Me._draw = New List(Of Integer)
        Dim whichNum As Integer
        For ct As Integer = 1 To numberOfBalls
            whichNum = PRNG.Next(Me._theHopper.Count)
            Me._draw.Add(Me._theHopper(whichNum))
            If Not DuplicatesAllowed Then Me._theHopper.RemoveAt(whichNum)
        Next
    End Sub

    Public Overrides Function ToString() As String
        If Me._draw Is Nothing Then
            Return Nothing
        Else
            Dim sb As New System.Text.StringBuilder
            For Each num As Integer In Me._draw
                sb.Append(num)
                sb.Append(" ")
            Next
            Return sb.ToString
        End If
    End Function

    Public ReadOnly Property Count() As Integer
        Get
            Return Me._draw.Count
        End Get
    End Property

    Public Function Item(itemNum As Integer) As Integer
        Return Me._draw(itemNum)
    End Function
End Class

这是根据维基中彩票的概念完成的

Public Class LotteryNum

    'This class create a set of lottery numbers in one group. 
    'Rule #1: Each number is from 1 to 49. 
    'Rule #2: No duplicates. 
    'Rule #3: Not ordered.
    'Rule #4: In one group. (No Mega Ball number)
    'Reference: http://en.wikipedia.org/wiki/Lottery

    Private Shared rand As New Random
    Private m As Integer = 5
    Private LowLimit As Integer = 1
    Private HighLimit As Integer = 49
    Private mNums As List(Of Integer)

    Sub New()
        Dim n As Integer
        mNums = New List(Of Integer)
        Do While mNums.Count <= m
            n = LowLimit + rand.Next(HighLimit - LowLimit + 1)
            If Not (mNums.Contains(n)) Then
                mNums.Add(n)
            End If
        Loop
    End Sub

    Sub New(ByVal GivenNums As Integer())
        If (GivenNums.Length <> 6) Then
            MsgBox("Input Lottery must contain 6 numbers ")
            Exit Sub
        End If

        mNums = New List(Of Integer)
        For Each n As Integer In GivenNums
            mNums.Add(n)
        Next
    End Sub

    Public ReadOnly Property Nums() As List(Of Integer)
        Get
            Return mNums
        End Get
    End Property

    Public Overrides Function ToString() As String
        Dim str As New List(Of String)
        For Each n As Integer In Nums
            str.Add(n.ToString)
        Next
        Return String.Join(", ", str.ToArray)
    End Function
    Public Shared Operator =(ByVal Lot1 As LotteryNum, ByVal Lot2 As LotteryNum) As Boolean
        For Each n1 As Integer In Lot1.Nums
            If Not (Lot2.Nums.Contains(n1)) Then
                Return False
            End If
        Next
        Return True
    End Operator
    Public Shared Operator <>(ByVal Lot1 As LotteryNum, ByVal Lot2 As LotteryNum) As Boolean
        Return Not (Lot1 = Lot2)
    End Operator

End Class

结束模块

VB与VB.NET不同。您使用的是VB.NET.Ok,但您能帮助我吗?我看不到任何地方您要将其转换为双精度,所以不。您能发布异常以及它发生在哪一行吗?我想是VBMath.Rnd()返回single@Bala-异常消息是关于
bulk
类的
ToString
方法的,不太可能是这样。
Public Class LotteryNum

    'This class create a set of lottery numbers in one group. 
    'Rule #1: Each number is from 1 to 49. 
    'Rule #2: No duplicates. 
    'Rule #3: Not ordered.
    'Rule #4: In one group. (No Mega Ball number)
    'Reference: http://en.wikipedia.org/wiki/Lottery

    Private Shared rand As New Random
    Private m As Integer = 5
    Private LowLimit As Integer = 1
    Private HighLimit As Integer = 49
    Private mNums As List(Of Integer)

    Sub New()
        Dim n As Integer
        mNums = New List(Of Integer)
        Do While mNums.Count <= m
            n = LowLimit + rand.Next(HighLimit - LowLimit + 1)
            If Not (mNums.Contains(n)) Then
                mNums.Add(n)
            End If
        Loop
    End Sub

    Sub New(ByVal GivenNums As Integer())
        If (GivenNums.Length <> 6) Then
            MsgBox("Input Lottery must contain 6 numbers ")
            Exit Sub
        End If

        mNums = New List(Of Integer)
        For Each n As Integer In GivenNums
            mNums.Add(n)
        Next
    End Sub

    Public ReadOnly Property Nums() As List(Of Integer)
        Get
            Return mNums
        End Get
    End Property

    Public Overrides Function ToString() As String
        Dim str As New List(Of String)
        For Each n As Integer In Nums
            str.Add(n.ToString)
        Next
        Return String.Join(", ", str.ToArray)
    End Function
    Public Shared Operator =(ByVal Lot1 As LotteryNum, ByVal Lot2 As LotteryNum) As Boolean
        For Each n1 As Integer In Lot1.Nums
            If Not (Lot2.Nums.Contains(n1)) Then
                Return False
            End If
        Next
        Return True
    End Operator
    Public Shared Operator <>(ByVal Lot1 As LotteryNum, ByVal Lot2 As LotteryNum) As Boolean
        Return Not (Lot1 = Lot2)
    End Operator

End Class
Sub Main()
    Dim lot1 As New LotteryNum
    Dim given As New LotteryNum(New Integer() {10, 18, 25, 33, 42, 7})
    Dim myNum As New LotteryNum(New Integer() {10, 25, 33, 42, 18, 7})
    Dim yourNum As New LotteryNum(New Integer() {10, 23, 33, 42, 18, 7})

    Console.WriteLine("The new lottery numbers are: {0}", lot1.ToString)
    Console.WriteLine("My lottery is the LOTTERY: {0}", (myNum = given))
    Console.WriteLine("Your lottery is the LOTTERY: {0}", (yourNum = given))

    'Generate 300 lotteries and write to file
    Dim str As New List(Of String)
    For i As Integer = 0 To 299
        str.Add((New LotteryNum).ToString)
    Next
    File.WriteAllLines("c:\temp\lottery.txt", str.ToArray)


    Console.ReadKey()

End Sub