【问题标题】:Output of function in Swift (integer array)Swift 中函数的输出(整数数组)
【发布时间】:2014-12-23 01:51:18
【问题描述】:
func calculateStatistics(scores:[Int]) -> (min: Int, max: Int, sum: Int)
{
    var max = scores[0]
    var min = scores[0]
    var sum = 0

    for score in scores
    {
        if score > max {
            max = score
        }

        else if score < min {
            min = score
        }

        sum += score
    }

    return (min, max, sum)
}

let numbers = calculateStatistics([5, 3, 100, 3, 9])
numbers

numbers 的输出是 (.0 3, .1 100, .2 120)。我想知道是否有人可以向我解释小数位?它看起来像数组的索引,但我不确定。

【问题讨论】:

    标签: arrays function swift indexing


    【解决方案1】:

    它们是元组的索引。您可以将元组的第一个值作为numbers.0 访问,第二个作为numbers.1 访问,第三个作为numbers.2 访问

    当然,您仍然可以使用个人名称:numbers.minnumbers.maxnumbers.sum

    您还可以使用_ 占位符仅检索其中的某些部分:

    let (min, max, _) = calculateStatistics([5, 3, 100, 3, 9])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-16
      • 2021-04-05
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      相关资源
      最近更新 更多