设计一程序,求解“韩信点兵”的问题,程序界面如下。输入士兵总人数在某个数以内,单击“点点看”按钮后,正确输出士兵的总人数。
vb 程序代码如下:
Private Sub Command1_Click()
n = Text1.Text
For i = 1 To n
If i Mod Text2.Text = Text3.Text Then
If i Mod Text4.Text = Text5.Text Then
If i Mod Text6.Text = Text7.Text Then
Text8.Text = i
End If
End If
End If
Next i
End Sub
当然可以用逻辑运算符and合并条件,具体程序代码如下:
For i = 1 To val(Text1.Text)
If i Mod val(Text2.Text) = val(Text3.Text) and i Mod val(Text4.Text) = val(Text5.Text) and i Mod val(Text6.Text) = val(Text7.Text) Then
Text8.Text = i
End If
Next i
End Sub
有没有加val()函数运算结果一样,但最好加val()函数,把获取的字符类型转化为数值类型。
如果不加val()也可以用dim先定义变量为数值类型。程序如下:
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer, f As Integer, n As Integer
n = Text1.Text
a = Text2.Text
b = Text3.Text
c = Text4.Text
d = Text5.Text
e = Text6.Text
f = Text7.Text
For i = 1 To n
If i Mod a = b And i Mod c = d And i Mod e = f Then
Text8.Text = i

End If
Next i
站点签名:世纪家园