你怎么在vb.net中找到5个中最多的?

san*_*een 4 vb.net max find

这是在3中找到最大值的代码,但我想找到最大值的代码5:

Dim a, b, c As Integer

a = InputBox("enter 1st no.") 
b = InputBox("enter 2nd no.") 
c = InputBox("enter 3rd no.")

If a > b Then 
    If a > c Then 
        MsgBox("A is Greater") 
    Else 
        MsgBox("C is greater") 
    End If 
Else 
    If b > c Then 
        MsgBox("B is Greater") 
    Else 
        MsgBox("C is Greater")
    End If 
End If 
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 13

将值放入数组并使用以下Max函数IEnumerable:

'Requires Linq for Max() function extension
Imports System.Linq
'This is needed for List
Imports System.Collections.Generic

' Create a list of Long values. 
Dim longs As New List(Of Long)(New Long() _
                                   {4294967296L, 466855135L, 81125L})

' Get the maximum value in the list. 
Dim max As Long = longs.Max()

' Display the result.
MsgBox("The largest number is " & max)

' This code produces the following output: 
' 
' The largest number is 4294967296
Run Code Online (Sandbox Code Playgroud)


Oli*_*bes 4

正如大卫所建议的,将你的价值观保留在一个列表中。这比使用单个变量更容易,并且可以扩展到所需的任意数量的值(最多数百万个值)。

如果出于某种原因需要保留单个变量,请执行以下操作:

Dim max As Integer = a
Dim name As String = "A"
If b > max Then
    max = b
    name = "B"
End If
If c > max Then
    max = c
    name = "C"
End If
If d > max Then
    max = d
    name = "D"
End If
' ...  extend to as many variables as you need.
MsgBox(name & " is greater")
Run Code Online (Sandbox Code Playgroud)