订购数组的问题从最重要到最少

时间:2015-12-30 04:43:27

标签: arrays vb.net random drag-and-drop

下面你将看到我制作随机数组的内容。我需要从最大到最小的顺序排序,然后根据用户选择的类将它们放入文本框中。这是一个DnD 4e能力发生器。我需要能够将最高能力分数放入该类最合适的领域。

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'Dim randstr As New Random

    'Dim a As Integer
    'Dim b As Integer
    'Dim c As Integer
    'Dim d As Integer
    'Dim h As Integer
    'Dim f As Integer


    'a = randstr.Next(3, 18)
    'b = randstr.Next(3, 18)
    'c = randstr.Next(3, 18)
    'd = randstr.Next(3, 18)
    'h = randstr.Next(3, 18)
    'f = randstr.Next(3, 18)

    Static randomNumberGenerator As New System.Random


    Dim randomNumbers(6) As Integer ' Create the array

    Dim smallestNumber As Integer = 3 ' Set the lower bounds

    Dim largestNumber As Integer = 18 ' Set the upper bounds



    For i = 0 To 6 ' loop through each element in the array

        randomNumbers(i) = randomNumberGenerator.Next(smallestNumber, largestNumber)

    Next

    If CbClass.SelectedItem = "Fighter" Then

    End If
End Sub

2 个答案:

答案 0 :(得分:0)

这会将您的数组从最大到最小排序 - 在此代码之后..

For i = 0 To 6 ' loop through each element in the array

    randomNumbers(i) = randomNumberGenerator.Next(smallestNumber, largestNumber)

Next

添加

Array.Sort(randomNumbers)
Array.Reverse(randomNumbers)

将它们添加到文本框

TextBox1.Text = randomNumbers(0).ToString
TextBox2.Text = randomNumbers(1).ToString
TextBox3.Text = randomNumbers(2).ToString
TextBox4.Text = randomNumbers(3).ToString
TextBox5.Text = randomNumbers(4).ToString
TextBox6.Text = randomNumbers(5).ToString

记住当然要将上述文本框的名称更改为你的名字。

要设置角色的统计数据,您可能需要这样的内容

If CbClass.SelectedItem = "Fighter" Then
    Strength = randomNumbers(0)
    Aglity = randomNumbers(1)
    Health = randomNumbers(2)
    Intellect = randomNumbers(3)
End If

等等。

答案 1 :(得分:0)

这是您生成和订购数字的最简单方法:

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

    Static randomNumberGenerator As New System.Random()

    Dim smallestNumber As Integer = 3 ' Set the lower bounds
    Dim largestNumber As Integer = 18 ' Set the upper bounds
    Dim randomNumbers() As Integer = _
        Enumerable _
            .Range(0, 6) _
            .Select(Function (x) randomNumberGenerator.Next(smallestNumber, largestNumber + 1)) _
            .OrderByDescending(Function (x) x) _
            .ToArray()

    ' Rest of your code here

End Sub

请注意,调用randomNumberGenerator.Next(smallestNumber, largestNumber + 1) + 1作为此次调用的第二个参数非常重要是独占上限。

运行此代码时,randomNumbers数组将包含以下内容:

randomNumbers

然后,您可以只分配Dim x = randomNumbers(0)

等值