1、打开VB6.0软件,新建一个标准exe工程;
2、在设计界面上添加一个Text控件和一个Command控件,Text1的属性Text设置为空,Command1的Caption属性设置为"判断";
3、双击Command1控件,进入代码编辑窗口,在代码编辑窗口输入如下代码:
Private Sub Command1_Click()
Dim n As Double
n = Val(Text1.Text)
If n > 0 Then
MsgBox (n & "是一个正数")
ElseIf n = 0 Then
MsgBox ("n等于0")
Else
MsgBox (n & "是一个负数")
End If
End Sub
4、点击运行工程,在Text1中输入一个数字,单击判断按钮,判断该数是正数、0还是负数;
Private Function JudgeUnsignInteger(strNum As String) As Boolean
JudgeUnsignInteger = False
On Error GoTo check1
Dim A As Double
Dim B As Integer
A = CDbl(strNum)
B = CInt(strNum)
If Len(CStr(A)) = Len(CStr(B)) Then
If B > 0 Then
JudgeUnsignInteger = True
End If
End If
check1:
End Function
这个是判断函数,是正数返回TRUE,不是返回FALSE
这个判断对形如22.00也判断为正确,不知这个算不算正数,如不算,改成如下形式.
Private Function JudgeUnsignInteger(strNum As String) As Boolean
JudgeUnsignInteger = False
On Error GoTo check1
Dim A As Double
Dim B As Integer
A = CDbl(strNum)
if len(cstr(A)) <> len(text1.text) then
exit function
end if
B = CInt(strNum)
If Len(CStr(A)) = Len(CStr(B)) Then
If B > 0 Then
JudgeUnsignInteger = True
End If
End If
check1:
End Function
先用IsNumeric判断是否为数。
再转化为int型,然后判断是否大于0