vb怎么求最大值和最小值

vb
334
2024/2/3 10:32:08
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在VB中,可以使用Math类的Max和Min方法来求最大值和最小值。

例如,下面的代码演示了如何求一个整数数组的最大值和最小值:

Dim numbers() As Integer = {10, 5, 20, 15, 8}
Dim maxValue As Integer = Math.Max(numbers)
Dim minValue As Integer = Math.Min(numbers)

Console.WriteLine("最大值: " & maxValue)
Console.WriteLine("最小值: " & minValue)

输出结果为:

最大值: 20
最小值: 5

另外,如果要求两个数的最大值和最小值,可以直接使用Math类的Max和Min方法:

Dim a As Integer = 10
Dim b As Integer = 5

Dim maxValue As Integer = Math.Max(a, b)
Dim minValue As Integer = Math.Min(a, b)

Console.WriteLine("最大值: " & maxValue)
Console.WriteLine("最小值: " & minValue)

输出结果为:

最大值: 10
最小值: 5

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: vb时间间隔设置的方法是什么