VB设计事件过程 假如公鸡5元一只,母鸡3元一只,3只小鸡一元。在文本框TEXT1中输入总金额, 5
假如公鸡5元一只,母鸡3元一只,3只小鸡一元。在文本框TEXT1中输入总金额,在文本框TEXT2中输入需购买的总鸡数。单击“计算方案”按钮,在标签Label2中输出购买的...
假如公鸡5元一只,母鸡3元一只,3只小鸡一元。在文本框TEXT1中输入总金额,在文本框TEXT2中输入需购买的总鸡数。单击“计算方案”按钮,在标签Label2中输出购买的方案。单击窗体任意位置,清除文本框Text1、文本框Text2和标签Label2的所有内容。程序运行界面如下图所示:
3.给窗体添加合适的事件代码,测试程序,使其实现上述功能。 展开
3.给窗体添加合适的事件代码,测试程序,使其实现上述功能。 展开
1个回答
展开全部
Dim bolStop As Boolean
'作者:星空无月(xkwy)
'编写时间:2013.8.30
Private Sub Command1_Click()
Dim boy As Integer, girl As Integer, child As Integer
Dim intMoney As Integer, intCount As Integer
Dim strPlan As String
Label2.Caption = ""
If CheckNum Then MsgBox "数据不合法,请重新输入!", vbCritical, "程序出错": Exit Sub
intMoney = Int(Val(Text1.Text))
intCount = Int(Val(Text2.Text))
For boy = 1 To intCount
For girl = 1 To intCount
For child = 1 To intCount
If CheckCanDO(boy, girl, child, intMoney, intCount) Then
strPlan = strPlan & OutText(boy, girl, child)
Me.Label2.Caption = strPlan
End If
If bolStop Then Exit Sub '设置退出(用户等的太急可以单击窗体退出)
DoEvents
Next
Next
Next
If Len(strPlan) <= 0 Then strPlan = "此条件下没有找到任何方案!"
Me.Label2.Caption = strPlan
MsgBox "完毕!"
End Sub
Private Sub Form_Click()
Label2.Caption = ""
Text1.Text = ""
Text2.Text = ""
bolStop = True
End Sub
Private Function CheckNum() As Boolean '检查数字是否有误
If Int(Val(Text1.Text)) <= 0 Then CheckNum = True: Exit Function
If Int(Val(Text2.Text)) <= 0 Then CheckNum = True: Exit Function
End Function
Private Function CheckCanDO(boy As Integer, girl As Integer, _
child As Integer, intMoney As Integer, _
intCount As Integer) As Boolean '检查此方案是否可行
If (boy * 5 + girl * 3 + child / 3 = intMoney) _
And boy + girl + child = intCount Then CheckCanDO = True
End Function
Private Function OutText(boy As Integer, _
girl As Integer, _
child As Integer) As String '输出文本
OutText = "公鸡:" & boy & "只×5元=" & boy * 5 & "元," & _
"母鸡:" & girl & "只×3元=" & girl * 3 & "元," & _
"小鸡:" & child & "只÷3元=" & child / 3 & "元" & vbCrLf
End Function
追问
这里面很多都错了吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询