
正文
关于vb.net的max函数的信息
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
vb.net程序,利用随机函数产生100个1到100之间的整数,求其中的最大最小值。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Randomize() '产生随机数种子 以防止每次生成结果都一样
Dim a(100) As Integer '声明数组存放数据 用来保存随机数结果
Dim max As Integer, min As Integer '声明两个变量存最大值和最小值
min = 1000 : max = 0 '设置初值
For i = 0 To 99 '循环100次
a(i) = Int(Rnd() * 1000 + 1) '用rnd生成一个随机数 由于rnd范围为0-1之间的小数.所以*1000设置他的范围为0到999之间 加1变成1到1000之间
If a(i) = max Then max = a(i) '如果当前数值大于最大值的变量就保存
If a(i) = min Then min = a(i) '如果当前数值小于最小值的变量就保存
Next
MsgBox("最大值为" max)
MsgBox("最小值为" min)
End Sub
相关问答
Q1: vb.NET一个找出数组最大最小值的程序有个小问题 谁看一下
vb.net的max函数你是不是应该对最大值和最小值赋初值(比如把 r(1) 赋给最大值和最小值)呢?不然最小值默认初始值是‘0’vb.net的max函数,后面vb.net的max函数的判断就不起作用了。你可以加个断点试试vb.net的max函数,他们vb.net的max函数的初始值是多少。。。
Q2: vb.net 数据库 最大值访问 MAX()
strSQL = "select MAX(ID) from caiming" 最后只返回了一个字段,不知道,vb.net的max函数你 dr.Item("IMG_url_add") 返回vb.net的max函数的是哪的东西?
按我的理解应该改成
strSQL = "select * from caiming where ID=(select MAX(ID) from caiming)"
Q3: vb.net中有没有类似max()这样的函数
首先vb.net是一种编程语言vb.net的max函数,它自己是没有的。
但vb.net与c#、vc++.net等其它编程语言共用一个公共类库(叫框架类库),这个类库在命名空间System中提供了一个类叫Math(Public NotInheritable Class Math)。它为三角函数、对数函数和其vb.net的max函数他通用数学函数提供了常数和静态方法,其中就包括Max。
Q4: 用vb.net求三个数中的最大值以及最小值函数的编写
MaxOrMin 指示返回最大还是最小.
Private Function Math(ByVal num1 As Integer, ByVal num2 As Integer, ByVal num3 As Integer, ByVal MaxOrMin As Short) As Integer
Dim s() As Integer = {num1, num2, num3}
Dim max, min As Integer
If s(0) s(1) Then
min = s(0)
max = s(1)
End If
If s(1) s(2) Then
min = s(1)
max = s(2)
End If
If MaxOrMin = 0 Then Return max
If MaxOrMin = 1 Then Return min
End Function
Q5: vb中max函数的调用方法
vb中没有这个函数,你可以自己写一个嘛。
public
function
max(a
as
single,
b
as
single)
as
single
if
a
b
then
max
=
a
else
max
=
b
end
function
这样你就可以使用它了,如
debug.print
max(12.5,16)
这时就会输出12.5与16中较大的一个数。
当然,你也可以把以上自定义函数中的两个变量,换成一个数组,这样就可以不只是在两个变量之间返回最大值,而是在一个数组中返回最大值。
如:
public
function
max(a()
as
single)
as
single
dim
i
as
integer,
p
as
single
p
=
a(1)
for
i
=
2
to
ubound(a)
if
p
a(i)
then
p
=
a(i)
next
i
max
=
p
end
function
这样你可以先定义一个数组,并把你要查找最大值的数据存入这个数组中,再调用这个函数查找。
如:
dim
dat()
as
single
dat(1)=...
dat(2)=...
...
...
debug.print
max(dat)
这样就可以输出一组数组中最大值。
vb.net的max函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、vb.net的max函数的信息别忘了在本站进行查找喔。







