for……next循环解决法:
Sub 百钱买百鸡_for( )
Dim cock As Long, hen As Long, chicken As Long
For cock = 1 To 20 '外循环每执行1次
For hen = 1 To 33 '内循环执行所有次---整个
chicken = 100 - cock - hen
If cock * 5 + hen * 3 + chicken / 3 = 100 Then
MsgBox "公鸡有" & cock & "只" & vbNewLine _
& "母鸡有" & hen & "只" & vbNewLine _
& "小鸡有" & chicken & "只"
End if
Next
Next
End Sub
do ……while解决法:
Sub 百钱买百鸡_do()
Dim cock As Long, hen As Long, chicken As Long, i As Long
Dim str As String
str = ""
cock = 1
i = 1
Do While (cock <= 20) '外循环每执行1次
cock = cock + 1
hen = 1
Do While (hen < 34) '内循环执行所有次---整个
chicken = 100 - cock - hen
If cock * 5 + hen * 3 + chicken / 3 = 100 Then
str = str & "第" & i & "情况:" & "公鸡有" & cock & "只" & vbNewLine _
& "母鸡有" & hen & "只" & vbNewLine _
& "小鸡有" & chicken & "只" & vbNewLine
i = i + 1
End If
hen = hen + 1
Loop
Loop
MsgBox str
End Sub