Kotlin 没有将变量识别为参数

Kotlin is not recognizing the variables as arguments

提问人:Ismael Matiz 提问时间:2/25/2023 更新时间:2/25/2023 访问量:24

问:

0

我正在做一个简单的问题石头,剪刀,纸,蜥蜴和斯波克从这个页面 https://retosdeprogramacion.com/semanales2023

我创建了一个函数调用 whoWins(),如果玩家 1 获胜,它基本上返回下一个字符串“P1”,如果玩家 2 获胜,则返回“P2”或“Tie”......你知道在什么情况下 XD

fun whoWins(game: Array<String>): String {

    var chooseP1 = game.get(0).toLowerCase()
    var chooseP2 = game.get(1).toLowerCase()

    println("Choose is ok : $chooseP1 and the type is: "+ chooseP1::class.simpleName)
    println("Choose is ok : $chooseP2 and the type is: "+ chooseP2::class.simpleName)

    var rules: MutableMap<String, ArrayList<String>> = mutableMapOf(
        "rock" to arrayListOf("scissors", "lizard"),
        "scissors" to arrayListOf("paper", "lizard"),
        "lizard" to arrayListOf("spock", "paper"),
        "paper" to arrayListOf("spock", "rock"),
        "spock" to arrayListOf("scissors", "rock")
    )

    if (chooseP1 == chooseP2) return "Tie"

    println("\nhere for some reason it returns null, HELP! "+ rules[chooseP1]+"\n")

    rules[chooseP1]?.forEach {
        if (it == chooseP2) {
            return "P1"
        }
    }

    return "P2"
}

出于某种原因,当我尝试使用 var 访问地图时,系统返回 null,

现在为了保存我的游戏的不同回合的问题,使用一个数组,但是当我访问该方法时,系统总是返回“P2”,但是当我使用该方法时,该方法直接在函数中声明了字符串,它工作正常,这是我的主要函数whoWins()

fun main(args: Array<String>) {
    var game1 = mapOf<String, Array<String>>(
        "round1" to arrayOf("RoCk", "ScIsSoRs️"),
        "round2" to arrayOf("scissors️", "rock"),
        "round3" to arrayOf("paper", "scissors️")
    )
    var game2 = arrayOf(
        arrayOf("Rock", "Scissors️"), arrayOf("scissors️", "rock"), arrayOf("paper", "scissors️"),
        arrayOf("spock", "scissors️"), arrayOf("lizard️", "spock"), arrayOf("spock", "rock️")
    )

    println("the var  ${game1.get("round1")!!.get(0)} type is: "+game1.get("round1")!!.get(0)::class.simpleName)

    println("Game 1 " + whoWins(game1.get("round1")!!))
    println("Game 2 " + whoWins(game2.get(0)!!))
    println("But the function is working fine: " + whoWins(arrayOf("ROCK", "SCISSORS")))
    /*
        for (game in game2){
            if(whoWins(game.get(0),game.get(1)) == "P1"){
                pointsP1++
            }else{
                pointsP2++
            }
        }

        println(
            if (pointsP1 == pointsP2) "Tie"
            else if (pointsP1 > pointsP2) "Player 1"
            else "Player 2"
        )
    */
}

在这种情况下,该函数应在此特定情况下返回“P1”,但这里是输出

the var RoCk type is: String
Choose is ok : rock and the type is: String
Choose is ok : scissors️ and the type is: String

here for some reason it returns null, HELP! null

Game 1 P2
Choose is ok : rock and the type is: String
Choose is ok : scissors️ and the type is: String

here for some reason it returns null, HELP! null

Game 2 P2
Choose is ok : rock and the type is: String
Choose is ok : scissors and the type is: String

here for some reason it returns null, HELP! [scissors, lizard]

But the function is working fine: P1

Process finished with exit code 0

这里还有编辑器中代码中的图片

enter image description here

enter image description here

enter image description here

enter image description here

我已经尝试过 ,并在功能中。如您所见,该条目已转换为小写HashMapMapMutableMapwhoWins()

函数 Kotlin 参数传递

评论

1赞 Tenfour04 2/25/2023
如果我将其粘贴到IDE中并运行它,它将正常工作。没有从映射返回的 null 值。你的字符串中可能有某种零宽度的字符,让你搞砸了。尝试将 IDE 的代码字体更改为 JetBrains Mono。它将在代码中显示零宽度字符。设置 ->编辑器 -> 字体。我认为 JetBrains Mono 带有 IntelliJ IDEA,但如果没有,您可以进行网络搜索。它是免费的。

答: 暂无答案