2015-12-30 149 views
0

下面你會看到我有什麼製作隨機數組。我需要從最大到最小的順序將項目排序,然後根據用戶選擇的類別將它們放入文本框中。這是一個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

非常非常不清楚你在問什麼。預期的結果是什麼?你會得到什麼?它怎麼錯了?參見[問] – Plutonix

+0

我沒有排序部分。我看了一些其他的代碼,但它沒有看到gor。我需要做的是將數組從高到低排序,然後能夠抓取我需要的數組的每個部分,然後將它們輸出到文本框中(如果它們滿足要求的話)。就像是有人制造戰鬥機一樣,最高的屬性需要力量。如果這是有道理的。 – Morrg

回答

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 

等。

+0

Ty sir就是我正在尋找的東西。 – Morrg

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)