小编Yus*_*eff的帖子

运算符调用对应于点限定调用“List.min().compareTo(500)”,这在可空接收器“List.min()”上是不允许的

以下函数会产生错误。如何在 if/for 语句中使用 let() 或类似的 null 检查函数。

这是我的代码:

fun main() {
    var List = listOf<Int>(201, 53 ,5 ,556 ,70 , 9999)
    var budget: Int = 500
    if(List.min() < 500) { // this line produces the error
        println("Yes you can buy from this shop")
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

Operator call corresponds to a dot-qualified call 'List.min().compareTo(500)' which is not allowed on a nullable receiver 'List.min()'.
Run Code Online (Sandbox Code Playgroud)

帮助我处理可空类型。谢谢

kotlin

4
推荐指数
1
解决办法
6626
查看次数

kotlin中如何更改字符串中的字符

我正在尝试将字符串中的字符更改为其他字符。

这是我的代码

fun main(args: Array<String>) {
    var str: String = "H...H"
    
    for(i in 0..str.length-1) {
        
        if( str[i] == '.') 
            str[i] = 'B'
    }
    println(ans)
    
}
Run Code Online (Sandbox Code Playgroud)

但这会产生错误:

jdoodle.kt:20:16: error: no set method providing array access
            str[i] = 'B'
Run Code Online (Sandbox Code Playgroud)

但下面的代码工作正常:

fun main(args: Array<String>) {
    var str: String = "H...H"
    var ans : String = ""
    for(i in 0..str.length-1) {
        if( str[i] == 'H') 
            ans += str[i]
        else if( str[i] == '.') 
            ans += 'B'
    }
    println(ans)
    
}
Run Code Online (Sandbox Code Playgroud)

我只想将字符串中的所有 ..... 更改为 B。

就像“H...H”到“HBBBH” …

kotlin

2
推荐指数
1
解决办法
4480
查看次数

标签 统计

kotlin ×2