Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Kotlin中切换数字的有效方法?_Kotlin - Fatal编程技术网

在Kotlin中切换数字的有效方法?

在Kotlin中切换数字的有效方法?,kotlin,Kotlin,我正在为Android开发一个Tic-Tac-Toe应用程序,我希望在轮换时在两个玩家(0-1)之间切换。0代表第一个玩家;第二个是1 实现这一目标的最简单方法是: if (currentPlayer == 1) { currentPlayer = 0 } else { currentPlayer = 1 } 但是,有没有更有效的方法与科特林合作?提前感谢这不是Kotlin的具体情况,只是很简单: currentPlayer = 1 - currentPlayer 该副本显示了一

我正在为Android开发一个Tic-Tac-Toe应用程序,我希望在轮换时在两个玩家(0-1)之间切换。0代表第一个玩家;第二个是1

实现这一目标的最简单方法是:

if (currentPlayer == 1) {
   currentPlayer = 0
} else {
   currentPlayer = 1
}

但是,有没有更有效的方法与科特林合作?提前感谢

这不是Kotlin的具体情况,只是很简单:

currentPlayer = 1 - currentPlayer

该副本显示了一种异或方法,看起来像Kotlin中的
currentPlayer=currentPlayer XOR 1
。但并不比减法短。