【问题标题】:VB.net. Find the 2 lowest values in an array and remove themVB.net。查找数组中的 2 个最小值并将其删除
【发布时间】:2010-01-07 23:23:29
【问题描述】:

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

公共部分类主窗体 公共子 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

【问题讨论】:

  • (6 - 1 + 1) 不等于 (6)
  • 对 Jason 的支持,以正确预测将选择“给我 codez”解决方案。
  • @autolykos,对于更敏锐的眼睛,(6 - 1 + 1) 实际上意味着“我正在使用 MSDN 公式”。 msdn.microsoft.com/en-us/library/e566zd96(VS.85).aspx

标签: arrays filter


【解决方案1】:

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

【讨论】:

  • 如果我对其进行排序,那么它是重新排列数组点还是仅重新排列值?如果数字保持不变,那么我可以放弃 Dicerolls(0) 和 Dicerolls (1)
  • Jedin,很明显你是编程新手。您应该尝试阅读您的教科书并思考您刚刚提出的问题,看看您是否可以自己解决。如果您无法推理,也许可以尝试使用 printline。
【解决方案2】:

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

        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

【讨论】:

  • 非常感谢!这就是我一直在寻找的。你这个摇滚人!
猜你喜欢
  • 2012-10-31
  • 1970-01-01
  • 2015-07-03
  • 2010-09-18
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
相关资源
最近更新 更多