【问题标题】:Increment Values in an array (Swift)增加数组中的值 (Swift)
【发布时间】:2019-11-20 14:30:59
【问题描述】:

所以我正在上课,我正在努力让我们的事情变得更容易一些。我们正在尝试制作像景观一样的 Q*bert,并且我们正在使用数组。我试图让它更短且可重复。所以我有:

let allCoordinate = world.allPossibleCoordinates
let coordinaterow = coordinate.row
var coordinaterow = 3

while coodinaterow != 8 {

    var heights: [Int] = [0,0,0,0,0,0,0,0,1]

    var index = 0

    for coordinate in allCoordinates {
        if coordinate.row ==3 {
            if index == height.count {
                index = 0
            }

            for i in 0...heights[index] {
                world.place(Block(), at: coordinate)
            }
            index += 1
        }
    }

    allCoordinate += 1
    coordinaterow += 1
}

如何增加高度数组,如:

0,0,0,0,0,0,0,0,1 -- 0,0,0,0,0,0,0,1,2 -- 0,0,0,0,0,0,1,2,3

等等

【问题讨论】:

    标签: arrays swift


    【解决方案1】:

    试试这个:

    func increment(array: [Int]) -> [Int] {
    
        var result = array
    
        var lastValue = false
    
        result = result.reversed().map { value in
            if lastValue { return 0 }
            if value == 0 {
                lastValue = true
                return 1
            }
            return value + 1
        }
        result.reverse()
        return result
    }
    

    【讨论】:

      【解决方案2】:

      您可以反向迭代您的集合索引并检查每个元素是否等于零,增加并中断迭代,否则只需增加它:

      var heights: [Int] = [0,0,0,0,0,0,0,1,2]
      
      for index in heights.indices.reversed() {
          if heights[index] == 0 {
              heights[index] += 1
              break
          }
          heights[index] += 1
      }
      
      print(heights)   // [0, 0, 0, 0, 0, 0, 1, 2, 3]
      

      【讨论】:

      • 在哪里插入 sn-p?我把它放在第 8 行,但是当我点击运行时,它加载然后什么也没发生,没有显示错误,它什么也不做,如果它更有意义的话,我在 iPad 上的 swift playground 应用程序上。会不会是因为这个?
      • @KevinBryant 随时编辑您的问题并添加任何其他信息。 iPad Playground 的行为方式应与上图相同
      【解决方案3】:

      获得高度渐变的另一种方法(这似乎是您想要实现的)不是考虑增加现有数组,而是创建它。下面的代码演示了这一点:

      let heightCount = 10
      let rampCount = heightCount // can be any value <= heightCount
      for ramp in 1...rampCount {
          let rampHeights = [Int](repeating: 0, count: heightCount-ramp) + [Int](1...ramp)
          print(rampHeights)
      }
      

      这段代码的输出将是

      [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
      [0, 0, 0, 0, 0, 0, 0, 0, 1, 2]
      [0, 0, 0, 0, 0, 0, 0, 1, 2, 3]
      [0, 0, 0, 0, 0, 0, 1, 2, 3, 4]
      [0, 0, 0, 0, 0, 1, 2, 3, 4, 5]
      [0, 0, 0, 0, 1, 2, 3, 4, 5, 6]
      [0, 0, 0, 1, 2, 3, 4, 5, 6, 7]
      [0, 0, 1, 2, 3, 4, 5, 6, 7, 8]
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      

      如您所见,heightCount 值决定了数组的总长度,而在循环内,ramp 值决定了 ramp 中非零值的数量。

      注意:循环仅用于演示目的。关键代码行是:

      let rampHeights = [Int](repeating: 0, count: heightCount-ramp) +
                        [Int](1...ramp)
      

      你可以把它放在你代码中任何你需要的地方。您只需要确保heightCountramp 具有适当的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-08
        • 1970-01-01
        • 1970-01-01
        • 2018-11-28
        相关资源
        最近更新 更多