求vb的一个编程

来源:百度知道 编辑:UC知道 时间:2024/09/25 12:06:56
请编写程序:实现除法运算
在文本框Text1中输入m,在文本框Text2中输入n,单击命令按钮“计算”实现:
1.如果Text1或Text2中不是正常数值(包含其它非数字字符),或其中一个为空,则Text3中显示Error
2.如果Text2中是0,则Text3中显示Error
3.否则将m÷n的值(四舍五入保留小数4位)放入Text3
(提示:正常情况下,判断文本框中是否为正常数值只要利用函数就可以了,不必使用循环)

Private Sub Command1_Click()
Dim m As Double
Dim n As Double

If Text1.Text = "" Or Text2.Text = "" Then
Text3.Text = "Error"
Exit Sub
ElseIf (Not IsNumeric(Text1.Text)) Or (Not IsNumeric(Text2.Text)) Then
Text3.Text = "Error"
Exit Sub
ElseIf Val(Text2.Text) = 0 Then
Text3.Text = "Error"
Exit Sub
Else
m = Val(Text1.Text)
n = Val(Text2.Text)
Text3.Text = Format$(Round(m / n * 10000 + 0.5) / 10000, "0.0000")
End If

End Sub

Option Explicit

Private Sub Command1_Click()
If IsNumeric(Text1) And IsNumeric(Text2) Then
If Val(Text2) <> 0 Then
Text3 = Format(Val(Text1) / Val(Text2), "0.0000")
Else
Text3 = "Error"
End If
Else
Text3 = "Error"
End If
End Sub