vbnet 当中比较一个数据与数组中的数据比它大的都去掉把比它最小的显示出来
你可以使用循环来比较一个数据与数组中的数据,并找到比它最小的数。以下是一个示例代码:
Sub Main()
Dim data As Integer = 5
Dim arr() As Integer = {2, 6, 1, 8, 3}
Dim min As Integer = Integer.MaxValue
For Each num As Integer In arr
If num < data AndAlso num < min Then
min = num
End If
Next
If min = Integer.MaxValue Then
Console.WriteLine("没有比{0}小的数", data)
Else
Console.WriteLine("比{0}小的最小数为: {1}", data, min)
End If
Console.ReadLine()
End Sub
在上面的代码中,我们首先定义了一个整数变量 data,并初始化为 5,然后定义了一个整数数组 arr。我们使用 For Each 循环遍历数组中的每个元素,如果元素比 data 小并且比当前的最小值 min 小,则将其赋值给 min。最后,我们检查 min 的值是否为 Integer.MaxValue,如果是,则表示没有比 data 小的数,否则输出 min 的值作为比 data 最小的数。
以上代码的输出结果为:比 5 小的最小数为: 2
原文地址: https://www.cveoy.top/t/topic/i3xq 著作权归作者所有。请勿转载和采集!