用VB实现上述功能的函数是:
Function fun(m, n) As Integer
Sum = 0
For i = 1 To 100
If i Mod m = 0 Or i Mod n = 0 Then
Sum = Sum + i
End If
Next i
fun = Sum
End Function
Private Sub Command1_Click()
m = CInt(InputBox("m="))
n = CInt(InputBox("n="))
Print "100以内能被"m"或"n"整除的数的总和="fun(m, n)
End Sub
Option Explicit
Private Sub Command1_Click()
Dim t, sum, a As Integer, n As Integer
Dim st As String, i As Integer
Label3.Caption = ""
a = HScroll1.Value
n = HScroll2.Value
For i = 1 To n
t = t * 10 + a
st = st & t & "+"
sum = sum + t
Next
Mid(st, Len(st), 1) = "="
Label3.Caption = Label3.Caption & st & sum
End Sub
Private Sub Command2_Click()
Dim t As Variant, sum As Variant
Dim a As Integer, n As Integer
Dim st As String, i As Integer
Label4.Caption = ""
a = HScroll1.Value
n = HScroll2.Value
For i = 1 To n
t = t * 10 + a
st = st & Format(t, "@@@@@@@@@@@@@") & vbNewLine
sum = sum + t
Next
Label4.Caption = Label4.Caption & st & _
"----------------" & vbCrLf & Format(sum, "@@@@@@@@@@@@@")
End Sub
Private Sub Form_Load()
HScroll1.Max = 9
HScroll1.Min = 1
HScroll1.SmallChange = 1
HScroll1.LargeChange = 3
HScroll1.Value = 1
HScroll2.Max = 10
HScroll2.Min = 5
HScroll2.SmallChange = 1
HScroll2.LargeChange = 3
HScroll2.Value = 5
Label1.Caption = "a=" & HScroll1.Value
Label2.Caption = "n=" & HScroll2.Value
Label3.Caption = ""
Label4.Caption = ""
Label4.Alignment = 2
Label3.AutoSize = True
End Sub
Private Sub HScroll1_Change()
Label1.Caption = "a=" & HScroll1.Value
End Sub
Private Sub HScroll2_Change()
Label2.Caption = "n=" & HScroll2.Value
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)