VB编程:鸡兔同笼问题
利用计算机解决古代数学问题“鸡兔同笼问题”。即已知在同一笼子里有鸡和兔的总头数为m头,鸡和兔的总脚数为n只,求鸡和兔各有多少只?另要求输入的数据必须经过检验,如果错误则作...
利用计算机解决古代数学问题“鸡兔同笼问题”。即已知在同一笼子里有鸡和兔的总头数为m头,鸡和兔的总脚数为n只,求鸡和兔各有多少只?另要求输入的数据必须经过检验,如果错误则作出提示,“确定”后自动清除错误数据并要求重新输入数据。触发事件发生在输入总脚数后焦点失去时,在文本框中输入数据;在图片框中输出结果,所有控件字体默认、大小为小四。 紧急啊啊啊啊。
展开
2个回答
展开全部
‘2个text 1个picture,纯体力活,自己修改:
Private Sub Form_Load()
With Text1
.Text = 100
.FontSize = 15
End With
With Text2
.Text = 240
.FontSize = 15
End With
Picture1.FontSize = 15
End Sub
Private Sub Text2_LostFocus()
Cls
If IsNumeric(Text1) And IsNumeric(Text2) Then
Dim i As Long, j As Long, m As Long, n As Long, flag As Boolean
m = Val(Text1): n = Val(Text2)
For i = 1 To m
For j = 1 To m - i
If i + j = m And i * 2 + j * 4 = n Then
Picture1.Print "鸡:"; i, "兔子:" & j
flag = True
End If
Next
Next
If Not flag Then Picture1.Print "无结果,检查输入"
Else
MsgBox "无效数字!"
Text1 = "": Text2 = ""
End If
End Sub
Private Sub Form_Load()
With Text1
.Text = 100
.FontSize = 15
End With
With Text2
.Text = 240
.FontSize = 15
End With
Picture1.FontSize = 15
End Sub
Private Sub Text2_LostFocus()
Cls
If IsNumeric(Text1) And IsNumeric(Text2) Then
Dim i As Long, j As Long, m As Long, n As Long, flag As Boolean
m = Val(Text1): n = Val(Text2)
For i = 1 To m
For j = 1 To m - i
If i + j = m And i * 2 + j * 4 = n Then
Picture1.Print "鸡:"; i, "兔子:" & j
flag = True
End If
Next
Next
If Not flag Then Picture1.Print "无结果,检查输入"
Else
MsgBox "无效数字!"
Text1 = "": Text2 = ""
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询