求编写程序,使用InputBox函数输入两个电阻的值,求它们并联和串联的电阻值,使用MsgBox消息框输出结果,要要求保留三位小数

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/09 12:38:09
求编写程序,使用InputBox函数输入两个电阻的值,求它们并联和串联的电阻值,使用MsgBox消息框输出结果,要要求保留三位小数

求编写程序,使用InputBox函数输入两个电阻的值,求它们并联和串联的电阻值,使用MsgBox消息框输出结果,要要求保留三位小数
求编写程序,使用InputBox函数输入两个电阻的值,求它们并联和串联的电阻值,使用MsgBox消息框输出结果,要
要求保留三位小数

求编写程序,使用InputBox函数输入两个电阻的值,求它们并联和串联的电阻值,使用MsgBox消息框输出结果,要要求保留三位小数
Sub Macro2()
Dim s1,s2 As String
Dim n5
Dim n1,n2,n3,n4 As Single
s1 = InputBox("请输入两电阻的阻值,之间用半角逗号“,”分隔","提示信息")
n1 = Val(s1)
n2 = Val(Mid(s1,InStr(s1,",") + 1))
If n1 > 0 And n2 > 0 Then
n3 = Round(n1 + n2,3) '串联电阻值
n4 = Round(n1 * n2 / (n1 + n2),3) '并联电阻值
n5 = MsgBox(n1 & "Ω和" & n2 & "Ω两个电阻的" & Chr(13) & "串联电阻值为:" & n3 & "Ω" & Chr(13) & "并联电阻值为:" & n4 & "Ω",,"计算结果")
Else
n5 = MsgBox("两电阻值输入有错,请重新输入!",,"错误提示")
End If
End Sub

Dim r1 As Double

Dim r2 As Double

Dim rc As Double

Dim rb As Double

Dim rsum As Double

Dim rmulti As Double

Private Sub Command1_Click()

Text1.Text = ""

Text2.Text = ""

Label6.Caption = ""

Label7.Caption = ""

End Sub

Private Sub Command2_Click()

rsum = r1 + r2

rmulti = r1 * r2

If rmulti <> 0 Then

rb = rmulti / rsum

Else

MsgBox "两个电阻值不能同为0!"

End If

Label6.Caption = rsum

Label7.Caption = rb

End Sub

Private Sub Form_Load()

Text1.Text = ""

Text2.Text = ""

End Sub

Private Sub Text1_Change()

r1 = Val((Text1.Text))

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If IsNumeric(Chr(KeyAscii)) = True Or KeyAscii = 8 Then

Else

KeyAscii = 0

End If

End Sub

Private Sub Text2_Change()

r2 = Val(Text2.Text)

End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)

If IsNumeric(Chr(KeyAscii)) = True Or KeyAscii = 8 Then

Else

KeyAscii = 0

End If

End Sub