【问题标题】:Write a function that receives two Int’s at the input and changes their values, and at the same time doubles them编写一个函数,在输入处接收两个 Int 并更改它们的值,同时将它们加倍
【发布时间】:2019-09-22 11:22:03
【问题描述】:

我请求帮助,我已经伤透了我的整个大脑。看来我明白这不是一项非常艰巨的任务。看了文档,上网搜索了半天。

编写一个函数,在输入端接收两个 Int 并更改它们的值,同时将它们加倍

【问题讨论】:

  • “改变他们的价值观” 怎么样?加倍?

标签: swift function


【解决方案1】:

我认为改变它们的值意味着交换它们然后加倍。如果是这样,您可以编写自己的函数:

func swapAndDouble(a: Int, b: Int) -> (Int, Int) {

    let returnTupple = (2*b, 2*a)

    return returnTupple
}

然后用这个调用:

let (first, second) = swapAndDouble(a: 3, b: 7)

你有结果。

或者,如果您想稍后将该函数用于其他值,标准 Swift 库中有一个标准交换函数。

func swap<T>(_ a: inout T, _ b: inout T)

请为此阅读Apple Documentation

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2022-02-08
    相关资源
    最近更新 更多