gpt4 book ai didi

java - 类型 'return length as Int.toString'的表达式 'Nothing'不能作为函数调用

转载 作者:行者123 更新时间:2023-12-02 13:28:30 25 4
gpt4 key购买 nike

private String getFileSize(long length) {
DecimalFormat df = new DecimalFormat("######0.0");
if (length < 1024.f) {
return (int) length + " B";
} else if (length < 1024 * 1024.f) {
return df.format(length / 1024.f) + " KB";
} else if (length < 1024 * 1024 * 1024.f) {
return df.format((length / 1024.f / 1024.f)) + " MB";
}
return df.format(length / 1024.f / 1024.f / 1024.f) + " GB";
}
原始码
private fun getFileSize(length: Long): String {
val df = DecimalFormat("######0.0")
if (length < 1024f) {
return length as Int.toString() + " B"
} else if (length < 1024 * 1024f) {
return df.format(length / 1024f.toDouble()) + " KB"
} else if (length < 1024 * 1024 * 1024f) {
return df.format((length / 1024f / 1024f).toDouble()) + " MB"
}
return df.format(length / 1024f / 1024f / 1024f.toDouble()) + " GB"
}
Java与Kotlin对话后的代码
return length as Int.toString() + " B"
错误:类型“Nothing”的表达式“返回长度为Int.toString”不能作为函数调用。找不到函数“invoke()”。 Unresolved reference :toString。

最佳答案

您正在将Int.toString方法称为datatype.toString方法,该方法没有任何效果,因此请使用String插值作为

return "$length B"
或使用 toString作为
return length.toString() + " B"
您还可以在 ${expression}内使用带有字符串插值的表达式作为
return "${df.format(length / 1024.f)} KB";

关于java - 类型 'return length as Int.toString'的表达式 'Nothing'不能作为函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62834811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com