vb里子函数如何定义,调用?
PrivateFunctionMaxmod_(ByValaAsLong,ByValbAsLong)_AsLongDimpAsLongp=aModbIfp=bThenMax...
Private Function Maxmod_(ByVal a As Long, ByVal b As Long) _
As Long
Dim p As Long
p = a Mod b
If p = b Then Maxmod = b
Else: Maxmod = Maxmod(b, p)
End Function
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim answer As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
answer = Maxmod(a, b)
Print answer
End Sub
我这个代码在运行的时候报错,说我Maxmod没有定义,为啥? 展开
As Long
Dim p As Long
p = a Mod b
If p = b Then Maxmod = b
Else: Maxmod = Maxmod(b, p)
End Function
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim answer As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
answer = Maxmod(a, b)
Print answer
End Sub
我这个代码在运行的时候报错,说我Maxmod没有定义,为啥? 展开
展开全部
先说下问题:
1,你调用的函数名和定义的函数名不一样 应改为一致
2,你的 if ...else 这个语句块不对,缺少 end if
3,你这是个递归函数,你的词句在不断的调用后,最终会使b(除数)为0.这样系统就会报错 应该在Maxmod()中添加一句当b=1时,让其退出函数.
修改后的函数如下,希望对你有所帮助
Private Function Maxmod(ByVal a As Long, ByVal b As Long) _
As Long
If b = 1 Then
Exit Function
End If
Dim p As Long
p = a Mod b
If p = b Then
Maxmod = b
Else
Maxmod = Maxmod(b, p)
End If
End Function
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim answer As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
answer = Maxmod(a, b)
Print answer
End Sub
1,你调用的函数名和定义的函数名不一样 应改为一致
2,你的 if ...else 这个语句块不对,缺少 end if
3,你这是个递归函数,你的词句在不断的调用后,最终会使b(除数)为0.这样系统就会报错 应该在Maxmod()中添加一句当b=1时,让其退出函数.
修改后的函数如下,希望对你有所帮助
Private Function Maxmod(ByVal a As Long, ByVal b As Long) _
As Long
If b = 1 Then
Exit Function
End If
Dim p As Long
p = a Mod b
If p = b Then
Maxmod = b
Else
Maxmod = Maxmod(b, p)
End If
End Function
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim answer As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
answer = Maxmod(a, b)
Print answer
End Sub
展开全部
1,你调用的函数名和定义的函数名不一样 应改为一致
2,你的 if ...else 这个语句块不对,缺少 end if
3,你这是个递归函数,你的词句在不断的调用后,最终会使b(除数)为0.这样系统就会报错 应该在Maxmod()中添加一句当b=1时,让其退出函数.
修改后的函数如下,希望对你有所帮助
Private Function Maxmod(ByVal a As Long, ByVal b As Long) _
As Long
If b = 1 Then
Exit Function
End If
Dim p As Long
p = a Mod b
If p = b Then
Maxmod = b
Else
Maxmod = Maxmod(b, p)
End If
End Function
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim answer As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
answer = Maxmod(a, b)
Print answer
End Sub
2,你的 if ...else 这个语句块不对,缺少 end if
3,你这是个递归函数,你的词句在不断的调用后,最终会使b(除数)为0.这样系统就会报错 应该在Maxmod()中添加一句当b=1时,让其退出函数.
修改后的函数如下,希望对你有所帮助
Private Function Maxmod(ByVal a As Long, ByVal b As Long) _
As Long
If b = 1 Then
Exit Function
End If
Dim p As Long
p = a Mod b
If p = b Then
Maxmod = b
Else
Maxmod = Maxmod(b, p)
End If
End Function
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim answer As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
answer = Maxmod(a, b)
Print answer
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码有问题 仔细点 我修改的 就不会错 你多个——
Private Function Maxmod(ByVal a As Long, ByVal b As Long) As Long
Dim p As Long
p = a Mod b
If p = b Then
Maxmod = b
Else
Maxmod = Maxmod(b, p)
end if
End Function
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim answer As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
answer = Maxmod(a, b)
Print answer
End Sub
Private Function Maxmod(ByVal a As Long, ByVal b As Long) As Long
Dim p As Long
p = a Mod b
If p = b Then
Maxmod = b
Else
Maxmod = Maxmod(b, p)
end if
End Function
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim answer As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
answer = Maxmod(a, b)
Print answer
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当然了,你定义的函数实际上是“Maxmod_”,多了个“_”
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |