【发布时间】:2017-06-23 15:23:29
【问题描述】:
我学习 swift atm 和 switch 语句是迄今为止最困难的部分。
下面的代码是为了简单地计算数组中的每一个投票,我希望它通过一个开关(!)来完成。
我的问题:
- 为什么操场一直跑? (重启了几次)
- 导入基础重要吗? (那行不是我的代码)
我更感兴趣的是它为什么不起作用以及它是如何工作的,而不是真正对正确的代码感兴趣。我很高兴有任何帮助!
import UIKit
enum SchoolMascotOption {
case salamander, marmot, neither
}
import Foundation
let mascotVotes: [SchoolMascotOption] = [.neither, .marmot, .salamander, .neither, .marmot, .neither, .neither, .marmot, .neither, .salamander, .salamander, .marmot, .neither, .neither, .salamander, .neither, .neither, .marmot, .salamander, .neither, .neither, .neither, .marmot, .marmot, .neither, .neither, .marmot, .salamander, .neither, .marmot, .marmot, .marmot, .marmot, .neither, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .marmot, .neither, .salamander, .salamander, .neither, .salamander, .neither, .salamander, .salamander, .salamander, .salamander, .salamander, .salamander, .marmot, .neither, .neither, .marmot, .salamander, .neither, .neither, .salamander, .salamander, .neither, .salamander, .salamander, .salamander, .salamander, .neither, .salamander, .neither, .salamander, .marmot, .salamander, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .marmot, .marmot, .marmot, .salamander, .marmot, .salamander, .marmot, .neither, .marmot, .neither, .salamander, .marmot, .marmot, .marmot, .neither, .marmot, .marmot, .salamander, .neither, .neither, .salamander, .neither, .neither, .marmot, .neither, .salamander, .salamander, .salamander, .neither, .neither, .salamander, .salamander, .salamander, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .marmot, .marmot, .neither, .neither, .salamander, .marmot, .neither, .marmot, .salamander, .salamander, .marmot, .salamander, .neither, .salamander, .marmot, .neither, .salamander, .marmot, .marmot, .salamander, .marmot, .salamander, .marmot, .salamander, .salamander, .marmot, .marmot, .neither, .marmot, .neither, .marmot, .salamander, .salamander, .salamander, .neither, .salamander, .salamander, .neither, .marmot, .neither, .marmot, .marmot, .marmot, .marmot, .neither, .marmot, .neither, .salamander, .marmot, .salamander, .neither, .salamander, .salamander, .marmot, .neither, .marmot, .neither, .salamander, .neither, .salamander, .neither, .neither, .marmot, .salamander, .neither, .marmot, .salamander, .marmot, .neither, .salamander, .neither, .neither, .salamander, .salamander, .salamander, .neither, .salamander, .neither, .marmot, .salamander, .marmot]
var salamanderCounts = 0
var marmotCounts = 0
var neitherCounts = 0
for vote in mascotVotes {
switch vote {
case .salamander :
salamanderCounts += 1
case .marmot :
marmotCounts += 1
case .neither :
neitherCounts += 1
}
}
print(salamanderCounts)
print(marmotCounts)
print(neitherCounts)
谢谢大家帮忙!!!
【问题讨论】:
-
τ代码,您向我们展示了按预期工作,在我的计算机上打印 70、60、62 :-)。这里不需要导入 UIKit 甚至 Foundation ...
-
谢谢,我试试看!也许我的 macbook 只是被这个巨大的任务压垮了,呵呵
标签: swift switch-statement swift-playground