swift - switch 语句:在 swift 中从 if 语句转换为 switch 语句
问题描述
我正在尝试从下面的 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")
}
将此开关块放入您的代码中:)
推荐阅读
- r - 如何在 R 的 emmeans 包中计算 SE
- javascript - PrimeNG FullCalendar 不工作,无法找到
- openid-connect - OIDC 登录重定向返回 500 错误 (signin-oidc)
- midi - 无法使用 music21 库在 Python 中解析 MIDI 文件
- css - 如何让 CSS 溢出和高度:计算在可调整大小的 div 中工作
- c# - 我想制作一个我自己的按钮,里面有两个其他按钮,我想让它们透明但透明度不起作用
- hyperledger-fabric - Hyperledger Fabric 中的私有数据是否适用读写原则?
- c# - c# DataGridView 复选框不会被选中
- python - Selenium - 元组对象不可调用
- html - 如何以角度获取条形图的滚动条