【发布时间】:2018-08-04 02:11:10
【问题描述】:
我正在尝试从下面的 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!")
}
这是我的 switch 语句:
switch meal {
let meal = "breakfast"
case 1:
print("Good morning!”)
case 2:
print("Good afternoon!")
case 3:
print("Good evening!")
}
【问题讨论】:
-
在
switch之前分配let meal = "breakfast"并且您的案例应该包括字符串:case "breakfast"等。您需要一个default语句。
标签: swift switch-statement control-flow