vb交换两个数大小

来源:百度知道 编辑:UC知道 时间:2024/06/07 23:37:12
输入N个数到数组中,找出其中最小的数和最大的数,并将两者互换位置。

Imports System
Module Tester

Sub Main()
Dim a, b As Integer
a = 1
b = 2
Swap1(a, b)
Console.WriteLine("{0} {1}", a, b)
Swap2(a, b)
Console.WriteLine("{0} {1}", a, b)
End Sub

' 方法一
Sub Swap1(ByRef lhs As Integer, ByRef rhs As Integer)
lhs -= rhs
rhs += lhs
lhs = rhs - lhs
End Sub

' 方法2
Sub Swap2(ByRef lhs As Integer, ByRef rhs As Integer)
lhs = lhs Xor rhs
rhs = rhs Xor lhs
lhs = lhs Xor rhs
End Sub

End Module

Option Explicit

Private Sub Form_Click()
Dim n, i As Integer
Dim a(), temp, x, max, min
Dim maxi, mini As Integer
n = InputBox("请输入元素的个数:", "数据输入", 5)
If n = "" Or IsNumeric(n) = False Then MsgBox "未输入数据或数据输入有误!", 64 + 0, "错误": Exit Sub
ReDim a(1 To n)
Print "数据的初始值是:"
For i =