你如何用 vb.net 在 5 中找到最大的数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15644712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you find greatest number among 5 with vb.net?
提问by sangeen
This is code for finding maximum in 3 but i want the code for finding maximum amoung 5:
这是在 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
采纳答案by Olivier Jacot-Descombes
As David suggested, keep your values in a list. That's easier than using individual variables and can be extended to as many values as requested (up to millions of values).
正如大卫所建议的,将你的价值观列在一个列表中。这比使用单个变量更容易,并且可以根据需要扩展到任意数量的值(最多数百万个值)。
If you need to keep individual variables for some reason, do this:
如果出于某种原因需要保留单个变量,请执行以下操作:
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")
回答by David
Put the values into an array and use the Max
function on IEnumerable
:
把值到一个数组,并使用该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
回答by Rajaprabhu Aravindasamy
A simple solution for you,
给你一个简单的解决方案,
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)