【问题标题】:A Kotlin function that takes two parameters, a String and an Int带有两个参数的 Kotlin 函数,一个 String 和一个 Int
【发布时间】:2022-12-20 04:31:23
【问题描述】:

我有个问题。如果有人能解决它,我将不胜感激。我已经在谷歌上搜索过了,也做了一些努力。

问题是: 创建一个函数,它接受两个参数,一个 String 和一个 Int,并返回一个包含 String 参数乘以 Int 参数的 String。该函数不应包含任何循环,并且应使用纯 Kotlin 编写。

使用 Kotlin 方法对我来说很重要。

【问题讨论】:

    标签: kotlin


    【解决方案1】:

    我自己解决了。

    fun myFunction(s1: String, n1: Int): String {
        return s1.repeat(n1)
    }
    

    【讨论】:

      【解决方案2】:
      fun stringIntMultiply (stringValue: String, intValue: Int): String = (stringValue.toInt() * intValue).toString()
      

      【讨论】:

      • 我收到此错误:类型不匹配:推断类型为 Int 但应为 String
      • 编辑检查它。
      猜你喜欢
      • 2012-01-21
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      相关资源
      最近更新 更多