温馨提示×

vb怎么求最大值和最小值

vb
小亿
225
2024-01-23 10:32:08
栏目: 编程语言

在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

0