在这段代码中,第2行遍历数组并输出它接收到的内容及其随机。但有时我会得到两次相同的东西,就像它会说"Straub",然后是"Straub“,然后是"Rusher”之类的东西。我试过做一个"do while循环“,但是我不知道如何设置它不重复的地方。顺便说一下,这是一种快速的编程语言。
let types = ["Alex", "Straub", "Rusher", "Graser"]
let type = types[Int(arc4random_uniform(UInt32(types.count)))]
println(type)如果您有任何问题,请张贴在评论部分。
发布于 2014-10-05 16:42:41
这避免了直接重复:
var lastIndex = -1
var index = -1
let types = ["Alex", "Straub", "Rusher", "Graser"]
do {
index = Int(arc4random_uniform(UInt32(types.count)))
} while index == lastIndex
println(types[index])
lastIndex = indexhttps://stackoverflow.com/questions/26204633
复制相似问题