我正在学习 Swift 语法,想知道为什么下面的代码没有像我期望的那样工作:
for i in 1...100{
switch (i){
case 1:
Int(i%3) == 0
println("Fizz")
case 2:
Int(i%5) == 0
println("Buzz")
default:
println("\(i)")
}
}
我想在每次数字能被 3(3、6、9、12 等)整除时打印 Fizz,并在每次能被 5 整除时打印 Buzz。拼图中缺少哪一块?
注意:我确实使用以下方法解决了这个问题:
for ( var i = 0; i < 101; i++){
if (Int(i%3) == 0){
println("Fizz")
} else if (Int(i%5) == 0){
println("Buzz")
} else {
println("\(i)")
}
}
我想知道如何使用 Switch 解决这个问题。谢谢你。
FizzBuzz 游戏的通常规则 将每个 3 的倍数替换为“Fizz”,将每个 5 的倍数替换为“Buzz”,并且 “FizzBuzz”的 3 和 5 的每一个倍数。
这可以通过元组上的 switch 语句来完成(i % 3, i % 5)
。 请注意,
_
表示“任何值”:
for i in 1 ... 100 {
switch (i % 3, i % 5) {
case (0, 0):
print("FizzBuzz")
case (0, _):
print("Fizz")
case (_, 0):
print("Buzz")
default:
print(i)
}
}
这允许您将与特定条件匹配的值(通过 where
子句评估)分配给临时变量(此处为
x
和
y
):
for i in 1...100 {
switch (i){
case let x where x%3 == 0:
println("Fizz")
case let y where y%5 == 0:
println("Buzz")
default:
println("\(i)")
}
}
您还可以使用案例主体中分配的临时值。
更新:
马特·吉布森(Matt Gibson)在评论中指出,如果您不打算在案例正文中使用临时变量,则可以省略对它的分配。
因此,上述代码的更简洁版本是:
for i in 1...100 {
switch (i){
case _ where i%3 == 0:
println("Fizz")
case _ where i%5 == 0:
println("Buzz")
default:
println("\(i)")
}
}
旁注:您的 2 个代码示例略有不同(第一个使用范围 0-100 作为输入,而第二个代码示例则在 1-100 上运行)。我的示例基于您的第一个代码片段。
switch
语句的人来说,这是一个更通用的答案。一般用法
switch someValue {
case valueOne:
// executable code
case valueTwo:
// executable code
default:
// executable code
}
示例
let someValue = "horse"
switch someValue {
case "horse":
print("eats grass")
case "wolf":
print("eats meat")
default:
print("no match")
}
备注:
break
声明。这是默认行为。 Swift
switch
案例不会“掉落”。如果您希望它们在下一种情况下继续执行代码,则必须显式使用
fallthrough
关键字。
break
语句。
case
语句不可行,可以最后包含
default
语句以捕获任何其他值。
switch
语句非常灵活。以下部分包括一些其他使用方法。匹配多个值
let someValue = "e"
switch someValue {
case "a", "b", "c":
// executable code
case "d", "e":
// executable code
default:
// executable code
}
间隔。
let someValue = 4
switch someValue {
case 0..<10:
// executable code
case 10...100:
// executable code
default:
// executable code
}
元组。此示例改编自值绑定
有时您可能想根据
switch
case
语句之后立即执行此操作。在任何使用值绑定的地方,它将匹配任何值。这与上面元组示例中使用
_
类似。以下两个示例是根据文档
修改的。
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
// can use x here
case (0, let y):
// can use y here
case let (x, y):
// can use x or y here, matches anything so no "default" case is necessary
}
您可以使用 where
关键字进一步细化匹配。
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
// executable code
case let (x, y) where x == -y:
// executable code
case let (x, y):
// executable code
}
进一步学习var i = 0
switch i {
case i where i % 5 == 0 && i % 3 == 0: print(" Fizz Buzz")
case i where i % 3 == 0 : print("Fizz")
case i where i % 5 == 0 : print("Buzz")
default: print(i)
}
交换机的行业标准行为可能会导致类似于
基本上,代码并不总是完全按照代码在阅读时看起来的方式执行,这导致代码审核员跳过关键错误。
为了反驳这一点,Apple 决定 switch 语句在 Swift 中的工作方式不应与行业标准相同。特别是:
每个案例结束时都会自动休息。不可能执行多个 case 语句。
1>>使用where关键字
func printNumberType(number : Int){
switch number {
case number where number % 3 == 0 && number % 5 == 0 :
print("foo bar")
case number where number % 3 == 0 :
print("foo")
case number where number % 5 == 0 :
print("bar")
default :
print("Number is not divisible by 3 and 5")
}
}
2>>使用值组合
func printNumberByMultipleValues(number : Int){
let combination = (number % 3,number % 5)
switch combination {
case (0,0):
print("foo bar")
case (0,_) :
print("foo")
case (_,0) :
print("bar")
default :
print("Number is not divisible by 3 and 5")
}
}
这是多值类型的更接近的示例。
var printNumberByMultipleValues : (Int)->() = { number in
let combination = (number % 3,number % 5)
switch combination {
case (0,0):
print("foo bar")
case (0,_) :
print("foo")
case (_,0) :
print("bar")
default :
print("Number is not divisible by 3 and 5")
}
}
for i in 1...100 {
switch (i.isMultiple(of: 3), i.isMultiple(of: 5)) {
case (true, true):
print("\(i) | FizzBuzz")
case (true, false):
print("\(i) | Fizz")
case (false, true):
print("\(i) | Buzz")
default:
print(i)
}
}
for i in 1...100 {
if i.isMultiple(of: 3) && i.isMultiple(of: 5) {
print("\(i) | FizzBuzz")
} else if i.isMultiple(of: 3) {
print("\(i) | Fizz")
} else if i.isMultiple(of: 5) {
print("\(i) | Buzz")
} else {
print(i)
}
}
class TEST1{
func print() -> Void{
var i = 0
for i in 1...100{
if Int(i%3) == 0 {
println("Fizz")
}
else if Int(i%5) == 0{
println("Buzz")
}
else {
println("\(i)")
}
}
}
}
var x = TEST1()
x.print()