switch 语句:在 swift 中从 if 语句转换为 switch 语句
switch statement: converting from if-statement to switch statement in swift
我正在尝试从下面的 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!")
}
let meal = "breakfast"
switch meal {
case "breakfast":
print("Good morning")
case "lunch":
print("Good afternoon!")
case "dinner":
print("Good evening!")
default:
print("default case")
}
将此开关块放入您的代码中:)
我正在尝试从下面的 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!")
}
let meal = "breakfast"
switch meal {
case "breakfast":
print("Good morning")
case "lunch":
print("Good afternoon!")
case "dinner":
print("Good evening!")
default:
print("default case")
}
将此开关块放入您的代码中:)