Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Random 为什么在使用nextInt()时添加0_Random_Kotlin - Fatal编程技术网

Random 为什么在使用nextInt()时添加0

Random 为什么在使用nextInt()时添加0,random,kotlin,Random,Kotlin,我正在跟随一位教练制作tic tac玩具游戏,让它自动播放 var r = Random() val randInt = r.nextInt(emptyCell.size-0) + 0 // adding 0 here 为什么我们需要在这里添加+0?在这种情况下,您没有理由写下+0nextInt返回一个Int,因此将0作为Int添加到其中绝对没有任何作用-不会改变类型或影响值-正如您所期望的那样 可能是教程中的一个输入错误。这是一个用于更改值的程序。作者刚刚向你们展示了它的位置和使用方法

我正在跟随一位教练制作tic tac玩具游戏,让它自动播放

var r = Random()
val randInt = r.nextInt(emptyCell.size-0) + 0    // adding 0 here

为什么我们需要在这里添加+0?

在这种情况下,您没有理由写下
+0
nextInt
返回一个
Int
,因此将0作为
Int
添加到其中绝对没有任何作用-不会改变类型或影响值-正如您所期望的那样


可能是教程中的一个输入错误。

这是一个用于更改值的
程序。作者刚刚向你们展示了它的位置和使用方法

下面是您的代码的外观:

var random = Random()
var randomIndex: Int?
randomIndex = random.nextInt(emptyCell.size - 1) + 2   // two values instead of 00
println("randomIndex $randomIndex")

val emptyCellId = emptyCell[randomIndex]
println("emptyCellId $emptyCellId")

var btnSelect: Button?
btnSelect = setButtonId(noOfCards, emptyCellId)

添加0将起作用,但不会更改任何内容

请注意,您使用的是Java,这会将代码限制在JVM中

如果您使用的代码将针对Kotlin所使用的所有平台,并且会更简单,因为您不需要实例化类

您可以这样使用它:

val randInt = Random.nextInt(emptyCell.size)

如果不需要指定债券或需要指定上限,请查看。

谢谢。明白了。那我们为什么不写r=random.nextInt(emptyCells.size)从中随机选择一个索引呢。直接?找到它的最简单方式——向作者提问——达摩·克舍特里(你可以在上面找到他)。