【问题标题】:Return the largest number in an array in VB.NET在 VB.NET 中返回数组中的最大数
【发布时间】:2022-01-01 11:44:58
【问题描述】:

我一直在尝试返回数组中的最大数字,但我一直得到 13。我在这里做错了什么?

    Dim maxage As Integer = 0
    Dim ageList() As Integer = {12, 13, 18, 11, 5, 3}
    Dim i As Integer

    For i = 0 To UBound(ageList)
        If i > maxage Then
            maxage = ageList(i)
        End If
    Next i
    Console.WriteLine(maxage)
    Console.ReadLine()

【问题讨论】:

  • 你可以试试 if ageList(i) > maxage then
  • 看起来这是 [VB.NET] 而不是 [VBA]。在 VB.NET 中,您也可以使用 {12, 13, 18, 11, 5, 3}.MaxageList.Max。在 VBA 中,您可以使用 WorksheetFunction.Max(ageList)

标签: arrays vb.net


【解决方案1】:

您将 'i' 与 'maxage' 进行比较,而不是数组中的元素。

Dim maxage As Integer = 0
Dim ageList() As Integer = {12, 13, 18, 11, 5, 3}
Dim i As Integer

For i = 0 To UBound(ageList)
    If ageList(i) > maxage Then
        maxage = ageList(i)
    End If
Next i
Console.WriteLine(maxage)
Console.ReadLine()

【讨论】:

  • 是的。由于maxage0 开始,i1 时将大于maxage。索引1 处的值是13,因此maxage 设置为13i 永远不会再大于 maxage。另外,我有一个侄子,名叫利未记。世界上不能有太多的名字;)
猜你喜欢
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
  • 2020-04-15
相关资源
最近更新 更多