程序代码:
Option Explicit Dim n(1 To 6) As Long Dim a(1 To 8) As Long Sub Init_a() a(1) = 1 a(2) = 3 a(3) = 5 a(4) = 8 a(5) = 16 a(6) = 24 a(7) = 32 a(8) = 40 End Sub Private Sub Command1_Click() 'n 的文本框是:text1(?) Dim i As Long Dim j As Long Dim b As Long Dim bc(1 To 96) As Long Dim bw As Long, bi As Long For i = 0 To 5 If IsNumeric(Text1(i).Text) Then n(i + 1) = Val(Text1(i).Text) Else MsgBox "第 " & i + 1 & " 个数输入不正确", vbCritical, "错误" Exit Sub End If Next i For i = 1 To 6 For j = 1 To 8 b = n(i) + a(j) '加法 If b <= 50 Then '符合条件 For bi = 1 To bw '搜索历史答案 If bc(bi) = b Then '找到 Exit Sub '退出循环 End If Next bi If bi = bw + 1 Then '如果没找到 bw = bw + 1 '计数 bc(bw) = b '保存历史 Text2.Text = Text2.Text & vbCrLf & n(i) & "+" & a(j) & "=" & b '添加结果 End If End If b = n(i) - a(j) '减法 If b >= 0 Then '符合条件 For bi = 1 To bw If bc(bi) = b Then Exit Sub End If Next bi If bi = bw + 1 Then bw = bw + 1 bc(bw) = b Text2.Text = Text2.Text & vbCrLf & n(i) & "-" & a(j) & "=" & b End If End If Next j Next i End Sub Private Sub Form_Load() Call Init_a End Sub
text2 是多行,带竖滚动条
text1 是控件数组,下标从 0 到 5
授人于鱼,不如授人于渔
早已停用QQ了