vb 求3个数的最大公约数问题
发布网友
发布时间:2023-05-31 17:55
我来回答
共5个回答
热心网友
时间:2024-01-22 20:20
'定义的是自定义函数,所以要这样写
Public Function gys(m, n)
Do
r = m Mod n
m = n
n = r
Loop Until r = 0
gys = m
End Function
'Command1_Click()里的变量都没有定义所以要定义好,如下
Private Sub Command1_Click()
Dim x As Integer, y As Integer, z As Integer
Dim t1 As Integer, t2 As Integer, t As Integer
x = Val(Text1.Text)
y = Val(Text2.Text)
z = Val(Text3.Text)
t1 = gys(x, z)
t2 = gys(x, y)
t = gys(t1, t2)
Print t;
End Sub
这样就可以显示了,我调试过了
热心网友
时间:2024-01-22 20:21
我帮你重写一个程序得了,你的程序麻烦,效率低啊!
既然用了函数,为何不做输入三个数直接得到结果呢?
Private Sub Command1_Click()
a = s(q, w, e)
End Sub
'下面为三个里求最大公约数的函数,使用时调用s()即可
Function s(y As Integer, u As Integer, i As Integer) As Integer
Dim m As Integer, n As Integer
If y > u Then m = u Else m = y
If m > i Then m = i
For n = m To 1 Step -1
If y Mod n = 0 And u Mod n = 0 And i Mod n = 0 Then
s = n
Exit For
End If
Next
End Function
热心网友
时间:2024-01-22 20:21
你把变量名写错了,把你的这一句代码t = gys(a1, a2)改为t = gys(t1, t2)
热心网友
时间:2024-01-22 20:22
补充一下:定义函数最好在函数后边也加上返回类型。
public function Gys( x1 as integer,x2 as integer) as integer
end function
热心网友
时间:2024-01-22 20:23
private function gys(m, n)
Do
r = m Mod n
m = n
n = r
Loop Until r = 0
gys = m
End
End function