我写了一个判断是否成年的VB程序,但运行到一半就退出了,这是为什么?请高手指点.

来源:百度知道 编辑:UC知道 时间:2024/07/02 04:23:58
代码在这里,请知道的速给我答复,因为急着用.
Private Sub Command1_Click()
Dim intAge As Integer
Text1.Text = intAge
If (intAge >= 150 Or intAge <= 0) Then
Text2.Text = "您输入的年龄超出允许的范围"
End '强制结束程序运行
End If
If (intAge < 18) Then
Text2.Text = "您现在是未成年人!"
End
End If
If (intAge >= 18) Then
Text2.Text = "你已经是成年人了!"
End
End If
End Sub
谢谢大家,可是我改了一下,第一个if的内容没执行,比如在text1中输入一个160,在text2中显示的是”你已成年”,这是为什么呀?

'你有两处错误,正确如下
Private Sub Command1_Click()
Dim intAge As Integer
intAge = Text1.Text
If intAge >= 150 Or intAge <= 0 Then
Text2.Text = "您输入的年龄超出允许的范围"

End If
If (intAge < 18) Then
Text2.Text = "您现在是未成年人!"

End If
If (intAge >= 18) Then
Text2.Text = "你已经是成年人了!"

End If
End Sub

Text1.Text = intAge

上面这个写反了!~

应该是 intAge=val(Text1.Text)

Text1.Text = intAge
改为:
intAge =Text1.Text

不然的话,你就是让文本框显示0, 且在下面第1个IF中,因为intAge 仍没赋值,为0,而结束程序

呵呵.. 你的END,都应该改成EXIT SUB

不过我帮你写了一个代码,呵呵,你可以看看呵:
Private Sub Command1_Click()

Dim intAge%
intAge = Val(Text1)

Text2.Text = IIf(intAge >= 150 Or intAge <= 0, "您输入的年龄超出允许的范围", IIf(intAge < 18, "您现在是未成年人!", "你已经是成年人了!"))

End Sub

1、将Text1.Text = intAge 改为intAge=val(text1.text