kotlin - 在 Kotlin 中,在函数中返回双精度时我不应该使用“return”吗?
问题描述
我可以构建应用程序,但我意识到我缺乏一些 Kotlin 知识。我注册了codewars,第一个问题如下:
找出为什么代码不起作用...
fun multiply(x: double, y: double) => x * y
我的答案
fun multiply(x: double, y: double) => Double {
return x * y
}
解决方案
没有像double
Kotlin 中那样的 keywork。Kotlin 对 double 类型使用 Class 类型实现(在 JVM 中它仍然使用原始 double 类型)
您可以按如下方式定义函数:
fun multiply(x: Double, y: Double): Double { return x * y }
或者如果您希望使用单语句方法,那么您可以定义如下
fun multiply(x: Double, y: Double) = x * y
推荐阅读
- c++ - 为什么优先级队列需要底层容器的front()、pop_back()而不是back()、pop_back()?
- java - 无法将 Parcelable 放入捆绑包中
- python - numpy 连接多个数组
- python - python:检查一个变量是否有值
- android - 当我们向下滚动recyclerview时如何更改工具栏的标题?
- c++ - 是否可以训练张量流生产模型?
- jquery - 在下拉 jquery 中选择
- sql - 在 SQL 中查找与键值对匹配的案例
- tensorflow - 没有名为“_pywrap_tensorflow_internal”的模块 - Tensorflow CPU 版本
- css - 在标题附近放置一条“线”,与标题大小、长度、填充或边距无关