switch语句:在swift中从if语句转换为switch语句
我正试图从下面的if语句转换为switch语句,但我一直遇到错误switch语句:在swift中从if语句转换为switch语句,swift,switch-statement,control-flow,Swift,Switch Statement,Control Flow,我正试图从下面的if语句转换为switch语句,但我一直遇到错误 let meal = "breakfast" if meal == "breakfast" { print("Good morning!") } else if meal == "lunch" { print("Good afternoon!") } else if meal == "dinner" { print("Good evening!") } else { print("Hello!")
let meal = "breakfast"
if meal == "breakfast" {
print("Good morning!")
} else if meal == "lunch" {
print("Good afternoon!")
} else if meal == "dinner" {
print("Good evening!")
} else {
print("Hello!")
}
这是我的转换声明:
switch meal {
let meal = "breakfast"
case 1:
print("Good morning!”)
case 2:
print("Good afternoon!")
case 3:
print("Good evening!")
}
将此开关块放在代码中:assign let mean=breaken在开关之前,您的案例应该包括字符串:case breaken等,并且您需要一个默认语句。
let meal = "breakfast"
switch meal {
case "breakfast":
print("Good morning")
case "lunch":
print("Good afternoon!")
case "dinner":
print("Good evening!")
default:
print("default case")
}