首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在vb.net中找到5中的最大数?

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

Stack Overflow用户
提问于 2013-03-27 02:28:07
回答 3查看 44.2K关注 0票数 5

这是求3的最大值的代码,但我想要求5的最大值的代码:

代码语言:javascript
复制
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 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-27 03:50:14

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

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

代码语言:javascript
复制
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")
票数 4
EN

Stack Overflow用户

发布于 2013-03-27 02:33:50

将值放入数组并使用the Max function on IEnumerable

代码语言:javascript
复制
'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
票数 17
EN

Stack Overflow用户

发布于 2013-03-27 03:42:00

一个简单的解决方案,

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15644712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档