也想过建立分别的函数进行处理,但是在写的时候出现问题,代码如下Dim i As Integer Dim s As Long, d As Long Public Function min(insu As Long) Cls i = i + 1 If i Mod 2 = 1 Then s = insu Print s ElseIf i Mod 2 = 0 Then d = insu Print d End IfIf i >= 2 ThenIf s > d Then min = d ElseIf d > s Then min = s End IfElse min = insu End If End FunctionPrivate Sub Command1_Click() Text2 = min(CLng(Text1.Text)) End Sub当不修改文本框1的输入的时候,按多次按钮,文本框2的最小值就变成空白,不知道错误在那里
Dim i As Integer Dim s As Long, d As Long Private Sub Form_Load() Dim s1, d1 As Long Timer1.Enabled = False Timer1.Interval = 40 Randomize s1 = Int((1000 * Rnd) + 1) d1 = Int((1000 * Rnd) + 1) If s1 > d1 Then s = s1: d = d1: Text1.Text = s: Text2.Text = d Else s = d1: d = s1: Text1.Text = d: Text2.Text = s End If Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Dim MyValue As Long Randomize MyValue = Int((1000 * Rnd) + 1) If MyValue > s Then s = MyValue: Text1.Text = s If MyValue < d Then d = MyValue: Text2.Text = dEnd Sub
改成一个函数不就行了 Dim i As Integer Dim s As Long, d As Long Private Sub Form_Load() Dim s1, d1 As Long Randomize s1 = Int((1000 * Rnd) + 1) d1 = Int((1000 * Rnd) + 1) If s1 > d1 Then s = s1: d = d1: Text1.Text = s: Text2.Text = d Else s = d1: d = s1: Text1.Text = d: Text2.Text = s End IfEnd SubPrivate Function XX() Dim MyValue As Long Randomize MyValue = Int((1000 * Rnd) + 1) If MyValue > s Then s = MyValue: Text1.Text = s If MyValue < d Then d = MyValue: Text2.Text = d End Sub Private Sub Command1_Click() XX End Sub
调用函数时给函数输入数据,函数处理,然后输出最大值和最小值 类似这样 Private Function XX(insu as single,max as single,min as single) '判断处理或者Function min(insu As Long)这样的函数形式
Private Function XX(insu As Single, max As Single, min As Single) Static Flag As Boolean Static sngMax As Single, sngMin As Single If Not Flag Then sngMax = insu sngMin = insu Flag = True Else If insu < sngMin Then sngMin = insu ElseIf insu > sngMax Then sngMax = insu End If End If
max = sngMax min = sngMin End FunctionPrivate Sub Command1_Click() Dim x As Single Dim y As Single Dim n As Single n = Rnd XX n, x, y Print n, x, y End Sub
if xx<a then a=xx
if yy>b then b=yy
Dim s As Long, d As Long
Public Function min(insu As Long)
Cls
i = i + 1
If i Mod 2 = 1 Then
s = insu
Print s
ElseIf i Mod 2 = 0 Then
d = insu
Print d
End IfIf i >= 2 ThenIf s > d Then
min = d
ElseIf d > s Then
min = s
End IfElse
min = insu
End If
End FunctionPrivate Sub Command1_Click()
Text2 = min(CLng(Text1.Text))
End Sub当不修改文本框1的输入的时候,按多次按钮,文本框2的最小值就变成空白,不知道错误在那里
Dim s As Long, d As Long
Private Sub Form_Load()
Dim s1, d1 As Long
Timer1.Enabled = False
Timer1.Interval = 40
Randomize
s1 = Int((1000 * Rnd) + 1)
d1 = Int((1000 * Rnd) + 1)
If s1 > d1 Then
s = s1: d = d1: Text1.Text = s: Text2.Text = d
Else
s = d1: d = s1: Text1.Text = d: Text2.Text = s
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim MyValue As Long
Randomize
MyValue = Int((1000 * Rnd) + 1)
If MyValue > s Then s = MyValue: Text1.Text = s
If MyValue < d Then d = MyValue: Text2.Text = dEnd Sub
Dim i As Integer
Dim s As Long, d As Long
Private Sub Form_Load()
Dim s1, d1 As Long
Randomize
s1 = Int((1000 * Rnd) + 1)
d1 = Int((1000 * Rnd) + 1)
If s1 > d1 Then
s = s1: d = d1: Text1.Text = s: Text2.Text = d
Else
s = d1: d = s1: Text1.Text = d: Text2.Text = s
End IfEnd SubPrivate Function XX()
Dim MyValue As Long
Randomize
MyValue = Int((1000 * Rnd) + 1)
If MyValue > s Then s = MyValue: Text1.Text = s
If MyValue < d Then d = MyValue: Text2.Text = d
End Sub
Private Sub Command1_Click()
XX
End Sub
类似这样
Private Function XX(insu as single,max as single,min as single)
'判断处理或者Function min(insu As Long)这样的函数形式
Static Flag As Boolean
Static sngMax As Single, sngMin As Single
If Not Flag Then
sngMax = insu
sngMin = insu
Flag = True
Else
If insu < sngMin Then
sngMin = insu
ElseIf insu > sngMax Then
sngMax = insu
End If
End If
max = sngMax
min = sngMin
End FunctionPrivate Sub Command1_Click()
Dim x As Single
Dim y As Single
Dim n As Single
n = Rnd
XX n, x, y
Print n, x, y
End Sub