迅速有能力通过陈述吗?例如,如果我执行以下操作
var testVar = "hello"
var result = 0
switch(testVal)
{
case "one":
result = 1
case "two":
result = 1
default:
result = 3
}
案例“一”和案例“二”是否可以执行相同的代码?
迅速有能力通过陈述吗?例如,如果我执行以下操作
var testVar = "hello"
var result = 0
switch(testVal)
{
case "one":
result = 1
case "two":
result = 1
default:
result = 3
}
案例“一”和案例“二”是否可以执行相同的代码?
Answers:
是。您可以按照以下方式进行操作:
var testVal = "hello"
var result = 0
switch testVal {
case "one", "two":
result = 1
default:
result = 3
}
另外,您可以使用fallthrough
关键字:
var testVal = "hello"
var result = 0
switch testVal {
case "one":
fallthrough
case "two":
result = 1
default:
result = 3
}
var testVar = "hello"
switch(testVar) {
case "hello":
println("hello match number 1")
fallthrough
case "two":
println("two in not hello however the above fallthrough automatically always picks the case following whether there is a match or not! To me this is wrong")
default:
println("Default")
}
fallthrough
大小写结尾处的关键字会导致您要寻找的掉线行为,并且可以在单个大小写中检查多个值。
fallthrough
,而且还建议使用多案例