求两道VB的作业题答案,请高手指教~

2024年11月18日 08:13
有2个网友回答
网友(1):

Private Sub Command1_Click()
For i = 1 To 7
x = Val(InputBox("输入星期" & i & "温度"))
Sum = Sum + x
Next i
MsgBox "平均温度为:" & Sum / 7
End Sub

======================

Private Sub Command1_Click()
Print 公约数(35, 14)
End Sub

Function 公约数(A As Long, B As Long)
Dim X As Long, Y As Long, K As Long
X = IIf(A >= B, A, B)
Y = IIf(A <= B, A, B)
Do
K = X Mod Y: If K = 0 Then Exit Do
X = Y: Y = K
Loop
公约数 = Y
End Function

网友(2):

1 :
For k = 1 To 7
l = l + CDbl(InputBox("请输入周" + CStr(k) + "温度"))
Next k
MsgBox l / 7

2 :
l1 = CLng(InputBox("请输入数1"))
l2 = CLng(InputBox("请输入数2"))

If l2 < l1 Then
l = l1: l1 = l2: l2 = l
End If

res = -1
For k = 1 To l1
If ((l1 Mod k) = 0) And ((l2 Mod k) = 0) Then res = k
Next k

If res > 1 Then
MsgBox "最大公约数=" + CStr(res)
Else
MsgBox "没有最大公约数"
End If