我可以缩短这段代码吗?

时间:2014-04-26 13:47:09

标签: vb.net visual-studio-2010 validation

我刚刚完成编程(建议我是这个编程的初学者)我想要制作一个程序,询问用户他们想要抛出一组三个6面模具的次数,一旦程序具有来自用户的投掷输入数量,它将找出每次投掷的结果并将其置于总整数中,以便可以将其移动到文本框以显示该数字在该期间出现的次数。卷的数量几乎像一个计数系统。现在我可能会以完全错误的方式执行此操作,或者我的逻辑可能不是编程方面最好的,但任何帮助都会很棒我将在下面发布我的代码

Public Class Form1
    Dim Numthrow As Integer
    Dim threetotal, fourtotal, fivetotal, sixtotal, sevtotal, eighttotal, ninetotal, tentotal, eeltotal As Integer
    Dim twtotal As Integer
    Dim threeteentotal As Integer
    Dim fourteentotal As Integer
    Dim fiveteentotal As Integer
    Dim sixteentotal As Integer
    Dim eightteentotal As Integer
    Dim nineteentotal As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Numthrow = TextBox1.Text

        For index = 0 To Numthrow
            Dim oDice As New Random
            Dim iDiceResult As Integer = oDice.Next(2, 19)

            If iDiceResult = 3 Then
                threetotal = threetotal + 1
            End If

            If iDiceResult = 4 Then
                fourtotal = fourtotal + 1
            End If

            If iDiceResult = 5 Then
                fivetotal = fivetotal + 1
            End If

            If iDiceResult = 6 Then
                sixtotal = sixtotal + 1
            End If

            If iDiceResult = 7 Then
                sevtotal = sevtotal + 1
            End If

            If iDiceResult = 8 Then
                eighttotal = eighttotal + 1
            End If

            If iDiceResult = 9 Then
                ninetotal = ninetotal + 1
            End If

            If iDiceResult = 10 Then
                tentotal = tentotal + 1
            End If

            If iDiceResult = 11 Then
                eeltotal = eeltotal + 1
            End If

            If iDiceResult = 12 Then
                twtotal = twtotal + 1
            End If

            If iDiceResult = 13 Then
                threeteentotal = threeteentotal + 1
            End If

            If iDiceResult = 14 Then
                fourteentotal = fourteentotal + 1
            End If

            If iDiceResult = 15 Then
                fiveteentotal = fiveteentotal + 1
            End If

            If iDiceResult = 16 Then
                sixteentotal = sixteentotal + 1
            End If

            If iDiceResult = 17 Then
                sevtotal = sevtotal + 1
            End If
            If iDiceResult = 18 Then
                eightteentotal = eightteentotal + 1
            End If
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        threetotal = 0
        fourtotal = 0
        fivetotal = 0
        sixtotal = 0
        sevtotal = 0
        eighttotal = 0
        ninetotal = 0
        tentotal = 0
        eeltotal = 0
        twtotal = 0
        threeteentotal = 0
        fourteentotal = 0
        fiveteentotal = 0
        sixteentotal = 0
        sevtotal = 0
        eightteentotal = 0
    End Sub
End Class

3 个答案:

答案 0 :(得分:1)

首先,您需要查看几个错误。

在循环中创建随机变量会导致在生成随机数时失败 See this question for details

接下来,在Random.Next(minValue, maxValue)来电中,minValue参数为包含,而maxValue独占。所以你从2开始随机数而不是3

最后,为了简化您的问题,您可以使用数组

' Defined at the global form level
Dim diceValue(18) As Integer
Dim randomGen As Random = new Random()

Private Sub Button1_Click(......)
    Numthrow = TextBox1.Text
    For index = 0 To Numthrow
       dim diceResult = randomGen.Next(3,19)
       diceValue(diceResult) += 1
    Next
End Sub

请注意,为了便于阅读,我将数组的大小设置为19个元素,如果你真的只使用了16个元素。索引0,1,2的前三个元素应该被丢弃

答案 1 :(得分:0)

您可以使用数组或列表,而不是拥有所有这些变量,而数组的索引将与您的总数相对应:

将它放在你的班级/模块中:

Private myTotals = New List(Of Integer)
Private oDice As New Random

这是你的事件处理程序:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Numthrow = TextBox1.Text

    For index = 0 To Numthrow
        Dim iDiceResult As Integer = oDice.Next(2, 19)
        myTotals(iDiceResult) += 1
    Next
End Sub

请注意,您只想实例化Random一次,因为如果不这样做,那么每次都会生成相同的数字。计算机无法真正生成随机数;他们从随机数字表中取出它们。

答案 2 :(得分:0)

使用班级

Public Class Form1
    Dim agame As New Dice(3, 6)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        agame.RollAll(100000)
        Dim temp As Dictionary(Of Integer, Integer)
        temp = agame.GetStats
        For Each diect As KeyValuePair(Of Integer, Integer) In temp
            Debug.WriteLine("{0}  {1}", diect.Key, diect.Value)
        Next
        agame.ClearStats()
    End Sub
End Class

Public Class CommonRandom
    Public Shared prng As New Random
End Class

Public Class Die
    Inherits CommonRandom
    Property Faces As Integer 'number of faces on die
    Property CurrentFace As Integer 'the current face
    Private _rollfaces As Integer 'for call to Random
    Private _stats As New Dictionary(Of Integer, Integer)

    Public Sub New(numberOfFaces As Integer)
        If numberOfFaces > 0 Then
            Me.Faces = numberOfFaces
            Me._rollfaces = numberOfFaces + 1
            Me.CurrentFace = 1
            'set up stat buckets
            For x As Integer = 1 To numberOfFaces
                Me._stats.Add(x, 0)
            Next
        Else
            Throw New ArgumentException("faces > 0")
        End If
    End Sub

    Public Function Roll() As Integer 'roll this die
        Me.CurrentFace = prng.Next(1, Me._rollfaces)
        Me._stats(Me.CurrentFace) += 1 'accum stats
        Return Me.CurrentFace
    End Function

    Public Function GetStats() As Dictionary(Of Integer, Integer)
        Return Me._stats
    End Function

    Public Sub ClearStats()
        For x As Integer = 1 To Me.Faces
            Me._stats(x) = 0
        Next
    End Sub
End Class

Public Class Dice
    Inherits CommonRandom
    Private Property Dice As New List(Of Die) 'holds die
    Private _stats As New Dictionary(Of Integer, Integer) 'stats

    Public Sub New(numberOfDie As Integer, numberOfFaces As Integer)
        If numberOfDie > 0 Then 'add die to collection
            For x As Integer = 1 To numberOfDie
                Me.Dice.Add(New Die(numberOfFaces))
            Next
        Else
            Throw New ArgumentException("number of die > 0")
        End If
    End Sub

    ''' <summary>
    ''' number of die
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function NumberOfDie() As Integer
        Return Me.Dice.Count
    End Function

    ''' <summary>
    ''' roll all die
    ''' </summary>
    ''' <param name="NumberOfTimesToRoll"></param>
    ''' <remarks></remarks>
    Public Sub RollAll(Optional NumberOfTimesToRoll As Integer = 1)
        For x As Integer = 1 To NumberOfTimesToRoll
            For Each d As Die In Me.Dice
                d.Roll()
            Next
        Next
    End Sub

    ''' <summary>
    ''' roll a random die
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function RollOneDie() As Integer
        Dim whDie As Integer = prng.Next(Me.Dice.Count)
        Return Me.Dice(whDie).Roll()
    End Function

    ''' <summary>
    ''' get the stats of all die
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetStats() As Dictionary(Of Integer, Integer)
        Dim temp As Dictionary(Of Integer, Integer)
        For Each d As Die In Me.Dice
            temp = d.GetStats
            For Each diect As KeyValuePair(Of Integer, Integer) In temp
                If Me._stats.ContainsKey(diect.Key) Then
                    Me._stats(diect.Key) += diect.Value
                Else
                    Me._stats.Add(diect.Key, diect.Value)
                End If
            Next
        Next
        Return Me._stats
    End Function

    ''' <summary>
    ''' clear the stats
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ClearStats()
        Me._stats.Clear()
        For Each d As Die In Me.Dice
            d.ClearStats()
        Next
    End Sub
End Class