Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vb.net中找到5个最大的数字?_Vb.net_Find_Max - Fatal编程技术网

如何在vb.net中找到5个最大的数字?

如何在vb.net中找到5个最大的数字?,vb.net,find,max,Vb.net,Find,Max,这是在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 g

这是在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 

将值放入数组并使用:


一个简单的解决方案

Dim xMaxNo As Integer
Dim xTemp As Integer

For i as integer = 1 To 5
   xTemp =  InputBox("enter No: " & i)
   xMaxNo = if(xTemp > xMaxNo, xTemp, xMaxNo)
Next

MsgBox("The Highest Number is " & xMaxNo)

正如David所建议的,将你的价值观列在一个列表中。这比使用单个变量更容易,并且可以根据请求扩展到任意多的值(多达数百万个值)

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

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")

要回答OP,你应该返回最大值的索引。@BradGonessurfing:我考虑过了,是的。实际上,这只是从链接的MSDN页面复制/粘贴的。虽然OP的代码并不清楚他到底在做什么或者为什么。然而,很明显,还有很大的改进空间。他肯定是个新手,但他的代码确实给出了最大数字的字母索引。事实上,在IEnumerable上做这件事并不像你想象的那么简单。Google MaxBy重要信息请注意
.Max()
函数需要
导入系统.Linq
Greate!!这就是我需要的,这是简单而优雅的。
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")