【问题标题】:No output in Swift PlaygroundSwift Playground 中没有输出
【发布时间】:2015-04-20 16:25:29
【问题描述】:

我正在尝试将斐波那契数放入数组中,并希望在操场控制台中查看数组输出,但由于某种原因,我没有看到任何输出。有人可以帮助我理解我在程序中犯的错误吗?

import UIKit

class FibonacciSequence {

    let includesZero: Bool
    let values: [Int]

    init(maxNumber: Int, includesZero: Bool) {
        self.includesZero = includesZero
        values = [0]
        var counter: Int
        if (includesZero == true) { counter = 0 }
        else { counter = 1 }
        for counter  <= maxNumber; {
            if ( counter == 0 ) {
                values.append(0)
                counter = 1
            }
            else {
                counter = counter + counter
                values.append(counter)
            }
        }
        println(values)

    }

    println(values)
    return values
}

let fibanocciSequence = FibonacciSequence(maxNumber:123, includesZero: true)

【问题讨论】:

    标签: ios swift swift-playground


    【解决方案1】:

    @ABakerSmith 已按原样为您详细介绍了代码中的问题,但您也可能需要考虑编写一个返回斐波那契数的 SequenceType,而不是初始化数组成员变量的类:

    struct FibonacciSequence: SequenceType {
        let maxNumber: Int
        let includesZero: Bool
    
        func generate() -> GeneratorOf<Int> {
            var (i, j) = includesZero ? (0,1) : (1,1)
            return GeneratorOf {
                (i, j) = (j, i+j)
                return (i < self.maxNumber) ? i : nil
            }
        }
    }
    
    let seq = FibonacciSequence(maxNumber: 20, includesZero: false)
    
    // no arrays were harmed in the generation of this for loop
    for num in seq {
        println(num)
    }
    
    // if you want it in array form:
    let array = Array(seq)
    

    如果您想提高多代的性能,当然可以记住序列。

    【讨论】:

      【解决方案2】:

      您的问题是您的代码中有错误;如果您的代码中有错误,Playgrounds 将不会运行它,您也不会得到任何输出。

      • for counter &lt;= maxNumber; 行上你有一个分号,但我很确定你不能像这样声明for 循环,除非我遗漏了什么?不过你可以使用while 循环。

      • 为什么要尝试从 init 方法返回 values

      • 您已将 values 声明为常量,但随后尝试使用 append 对其进行更改。

      • 使用此代码并修复所述错误不会产生斐波那契数列,而是会产生:[0, 0, 2, 4, 8, 16, 32, 64, 128]

      试试这个代码:

      class FibonacciSequence {
          let values: [Int]
      
          init(maxNumber: Int, includesZero: Bool) {
              var tempValues = includesZero ? [0] : [1]
              var current = 1
      
              do {
                  tempValues.append(current)
      
                  let nMinus2 = tempValues[tempValues.count - 2]
                  let nMinus1 = tempValues[tempValues.count - 1]
      
                  current = nMinus2 + nMinus1
      
              } while current <= maxNumber
      
              self.values = tempValues
          }
      }
      

      然后创建一个实例:

      let fibanocciSequence = FibonacciSequence(maxNumber:123, includesZero: true)
      println(fibanocciSequence.values) // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
      

      希望有帮助!

      【讨论】:

      • 无需强制解包last——可选项可与非可选项进行比较。
      • 干杯,我已经修改了答案。
      猜你喜欢
      • 1970-01-01
      • 2023-04-11
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多