请问VB中 select case 的用法

select case year
case year mod 400 =0
这个语法会错吗?
还有,CASE YEAR 和IF 的用法有什么区别啊?是不是都可以用?
我想问下一下if代码转为select case应该怎么写
Dim year as integer
year=val(text1.text)
if year mod 400=0 then
b="闰年"
elseif year mod 100<>0 and year mod 4= 0 then
b="闰年"
else
b="不是闰年"
end if
text2.text=b
end sub

这里不是不能用select case写,而是if和select case适用于不同的选择中。在这道题中,使用if显得条理很清楚,使用的语句也少。而使用select case显得太麻烦和不可理喻,请看:
rivate Sub Form_Click()
Dim year As Integer, b As String
year = Val(Text1.Text)
Dim x400 As Integer, x100 As Integer, x4 As Integer
x400 = year Mod 400
x100 = year Mod 100
x4 = year Mod 4
Select Case x400
Case 0
b = "闰年"
Case Else
Select Case x100
Case 0
b = "不是闰年"
Case Else
Select Case x4
Case 0
b = "闰年"
Case Else
b = "不是闰年"
End Select
End Select
End Select
Text2.Text = b
End Sub
简直是一材乱麻,做你这道题,我自己都快搞晕了,不过执行起来是完全正确的。已经运行过了。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-03-31
不对的,case是对一个值的大小范围检测的,你这里不是对year检测。而是对 year mod 400=0 这个逻辑计算式进行检测!
case 里面只能是:case 数值 、case is >(<,>=,<=)数值、case 数值 to 数值
第2个回答  2009-03-31
Dim day As String = "2"

Select Case day
Case "1"
<----------------->
Case "2"
<----------------->
Case "3"
<----------------->
Case "4"
<----------------->
End Select

用IF的话

if day = "1" then
<----------------->
elseif day = "2" then
<----------------->
elseif day = "3" then
<----------------->
elseif day = "4" then
<----------------->
end if
第3个回答  2019-01-28
private
sub
command1_click()
dim
k
as
integer
k
=
val(text1.text)
select
case
k
case
is
>=
90
text2.text
=
"a"
case
80
to
90
text2.text
=
"b"
case
70
to
80
text2.text
=
"c"
case
60
to
70
text2.text
=
"d"
case
is
<
60
text2.text
=
"e"
end
select
end
sub
第4个回答  2019-11-27
Select
Case
表达式
Case
值1
程序1
Case
值2
程序2.
.
.
Case
Else
其他程序
End
Select

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网