这里有两种方法。第一种,直接交换,代码比较多,容易出错。
第二种,把交换部分写成一个独立过程,中间调用。便于阅读,书写方便,不易出错。
用户的关于从大到小和从小到大排序选择,使用Option控件()如图。
Private Sub Command1_Click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim tmp As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
If Option1.Value = True Then
If a < b Then
tmp = a
a = b
b = tmp
End If
If c > a Then
tmp = b
b = c
c = tmp
tmp = a
a = b
b = tmp
ElseIf c > b Then
tmp = b
b = c
c = tmp
End If
Else
If a > b Then
tmp = a
a = b
b = tmp
End If
If c < a Then
tmp = b
b = c
c = tmp
tmp = a
a = b
b = tmp
ElseIf c < b Then
tmp = b
b = c
c = tmp
End If
End If
Text1.Text = CStr(a)
Text2.Text = CStr(b)
Text3.Text = CStr(c)
End Sub
Sub swap(x, y)
Dim tmp
tmp = x
x = y
y = tmp
End Sub
Private Sub Command2_Click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim tmp As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
If Option1.Value = True Then
If a < b Then
End If
If c > a Then
swap b, c
swap a, b
ElseIf c > b Then
swap b, c
End If
Else
If a > b Then
swap a, b
End If
If c < a Then
swap b, c
swap a, b
ElseIf c < b Then
swap b, c
End If
End If
Text1.Text = CStr(a)
Text2.Text = CStr(b)
Text3.Text = CStr(c)
End Sub