if语句你换下行
把
If x >= 0 And (x Mod 1) = 0 And y >= 0 And (y Mod 1) = 0 Then Text3 = x And Text4 = y
ElseIf x < 0 Or (x Mod 1) <> 0 Or y < 0 Or (y Mod 1) <> 0 Then InputBox ("出错")
End If
改成
If x >= 0 And (x Mod 1) = 0 And y >= 0 And (y Mod 1) = 0 Then
Text3 = x And Text4 = y
ElseIf x < 0 Or (x Mod 1) <> 0 Or y < 0 Or (y Mod 1) <> 0 Then
InputBox ("出错")
End If
‘2个text 1个picture,纯体力活,自己修改:
Private Sub Form_Load()
With Text1
Text = 100
FontSize = 15
End With
With Text2
Text = 240
FontSize = 15
End With
Picture1FontSize = 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
Picture1Print "鸡:"; i, "兔子:" & j
flag = True
End If
Next
Next
If Not flag Then Picture1Print "无结果,检查输入"
Else
MsgBox "无效数字!"
Text1 = "": Text2 = ""
End If
End Sub
窗体设计可以按照我的截图!控件就不说了!很简单!添加控件后直接把代码复制进去就行了!
代码如下:'判断数据是否是整数!
Public Function isInt(s As String) As Boolean
If CStr(CInt(Val(s))) = s Then
isInt = True
Else
isInt = False
End If
End Function
'根据头数和脚数求解兔子的数量的方程
Public Function Solv(head As Integer, foot As Integer) As Integer
Dim Rabit As Integer
Rabit = (foot - head 2) / 2
Solv = Rabit
End Function
Private Sub Command1_Click()
Dim err As String, OK As String
Dim h_num As Integer, f_num As Integer
If isInt(Text1Text) Then
h_num = CInt(Text1Text)
Else
err = err + "头数不是正整数!"
End If
If isInt(Text2Text) Then
f_num = CInt(Text2Text)
Else
err = err + "脚数不是正整数!"
End If
If err = "" Then
OK = OK & "兔子数量为:" & CStr(Solv(h_num, f_num)) & vbCrLf
OK = OK & "鸡的数量为:" & CStr(h_num - Solv(h_num, f_num)) & vbCrLf
MsgBox OK, vbOKOnly, "结果显示"
Else
MsgBox err, vbOKOnly, "出错了"
End If
End Sub
建立两个textbox控件用于输入头和脚的数量,两个label控件,用于显示鸡和兔的数量
一个按钮command控件
Private Sub Command1_Click()
tou = Val(Text1Text)
jiao = Val(Text2Text)
For tu = 0 To tou
ji = tou - tu
jijiao = ji 2
tujiao = tu 4
If jiao = jijiao + tujiao Then
Label5Caption = Str(ji)
Label6Caption = Str(tu)
End If
Next
End Sub
dim n as integer,n as integer
m=val(inputbox("输入鸡兔总数:"))
do
n=val(inputbox("输入鸡兔总脚数:"))
if n>=2m and n<=4m and (n mod 2=0) then
exit do
else
msgbox "输入的数据无效!请重新输入!"
end if
loop
print "共有鸡";(4m-n)/2;"只,共有兔";(n-2m)/2;"只。"
以上就是关于VB 鸡兔同笼全部的内容,包括:VB 鸡兔同笼、VB编程:鸡兔同笼问题、vb解决鸡兔同笼问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)