Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim arr As New ArrayList
Dim max As Integer
arr.Add(CInt(TextBox1.Text))
arr.Add(CInt(TextBox2.Text))
arr.Add(CInt(TextBox3.Text))
'排序(从小到大)
arr.Sort()
'最大值
max = arr(arr.Count - 1)
求最大数:
function getmax(byref a() as integer) as integer
dim i as integer, max as integer
max = a(0)
for i = 1 to ub……
[/Quote]
无论是byref还是byval,都应该显式书写。
为什么没有中间大的。因为你没有问,而且这里用不到。如果你需要找第二大的,第三大的,那么排序下好了。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arr(2) As Integer
dim max as integer
arr(0) = CInt(TextBox1.Text)
arr(1) = CInt(TextBox2.Text)
arr(2) = CInt(TextBox3.Text)
max = getmax(arr)
End Sub
你知道了最大的边,但你怎么知道其他的两边到底是arr(0),arr(1),arr(2)中那个呢?
求最大数:
function getmax(byref a() as integer) as integer
dim i as integer, max as integer
max = a(0)
for i = 1 to ubound(a)
if ……
[/Quote]
为什么要用byref呢?用byval也可以呀,我试过了。这两个有什么区别吗?
还有,谢谢2楼的回答,但你们都没有告诉我怎么判断哪个是中间大的,例如四个数中第二个大的。
是不是比较大小一定要放在数组中才能进行?
如果可能的话,还请介绍一下在数组中排序的方法(我听说过有个冒泡法,但看不懂书上的)。
求最大数:
function getmax(byref a() as integer) as integer
dim i as integer, max as integer
max = a(0)
for i = 1 to ubound(a)
if max < a(i) max = a(i)
next
getmax = max
end function