VB程序设计练习题?
设计界面:
代码:
Private Sub Command1_Click()
Dim n As Long
n = Val(InputBox("输入", "请输入您个年龄", 12))
If n < 18 Then
Print n; "少年"
ElseIf n >= 18 And n < 45 Then
Print n; "青年"
ElseIf n >= 45 And n <= 59 Then
Print n; "中年"
ElseIf n > 59 Then
Print n; "老年"
Else
Print n; "输入年龄错误"
End If
End Sub
Private Sub Command2_Click()
Dim n As Long
n = Val(InputBox("输入", "请输入您个年龄", 12))
Select Case n
Case Is < 18
Print n; "少年"
Case Is > 59
Print n; "老年"
Case Is >= 18
Select Case n
Case Is < 45
Print n; "青年"
Case Else
Print n; "中年"
End Select
Case Else
Print n; "输入年龄错误"
End Select
End Sub
2023-06-12 广告
DimnAsLong
n=Val(InputBox("输入","请输入您个年龄",12))
Ifn<18Then
Printn;"少年"
ElseIfn>=18Andn<45Then
Printn;"青年"
ElseIfn>=45Andn<=59Then
Printn;"中年"
ElseIfn>59Then
Printn;"老年"
Else
Printn;"输入年龄错误"
EndIf
EndSub
PrivateSubCommand2_Click()
DimnAsLong
n=Val(InputBox("输入","请输入您个年龄",12))
SelectCasen
CaseIs<18
Printn;"少年"
CaseIs>59
Printn;"老年"
CaseIs>=18
SelectCasen
CaseIs<45
Printn;"青年"
CaseElse
Printn;"中年"
EndSelect
CaseElse
Printn;"输入年龄错误"
EndSelect
EndSub
PrivateSubForm_Load()
Me.Caption="年龄段划分"
Command1.Caption="使用if"
Command2.Caption="使用select"
EndSub