VB.net。找到数组中的2个最低值并将其删除

时间:2010-01-07 23:23:29

标签: arrays filter

我有一个为Die roller创建的简单数组。 我想掷5个六面骰子,并删除最低的2个值。 什么代码可以帮助我做到这一点。 这是我骰子的基本代码

Public Partial Class MainForm     Public Sub New()

    Me.InitializeComponent()

End Sub

Sub Button1Click(sender As Object, e As EventArgs)
    Dim d61 as Integer
    Dim d62 As Integer
    Dim d63 As Integer
    Dim d64 As Integer
    Dim d65 As Integer

    d61 = Int((6 - 1 + 1) * Rnd) + 1
    d62 = Int((6 - 1 + 1) * Rnd) + 1
    d63 = Int((6 - 1 + 1) * Rnd) + 1
    d64 = Int((6 - 1 + 1) * Rnd) + 1
    d65 = Int((6 - 1 + 1) * Rnd) + 1

    Dim Dicerolls(4) As Integer
        Dicerolls(0) = d61
        Dicerolls(1) = d62
        Dicerolls(2) = d63
        Dicerolls(3) = d64
        Dicerolls(4) = d65

2 个答案:

答案 0 :(得分:2)

你可以sort数组并删除前两个元素。

答案 1 :(得分:1)

这是使用通用列表来完成工作的代码。

        Imports System.Collections.Generic
        Public Function GenerateRolls() As List(Of Integer)
            Dim diceCount As Integer = 5
            Dim rolls As List(Of Integer) = New List(Of Integer)

            Randomize() 'This will randomize your numbers'
            For i As Integer = 0 To diceCount
                rolls.Add(CInt(6 * Rnd()) + 1)
            Next

            rolls.Sort() 'sorts the array in ascending order.'

            'removes the two lowest rolls'
            rolls.RemoveAt(0)
            rolls.RemoveAt(0)

            'Write out all rolls to console'
            For i As Integer = 0 To rolls.Count - 1
                Console.WriteLine(rolls(i).ToString())
            Next

            Return rolls
        End Function