我有一个生成随机问题的测验应用程序。目前由于随机化,您有时可以多次回答同一个问题。我有一系列问题,我使用 arc4Random 方法在屏幕上生成一个随机问题。
我如何才能让 swift 识别出一个问题已经存在并跳过它?我必须做一个 if 声明吗?如果是这样,我将如何编码?
var currentQuestion = Int(arc4random_uniform(31) + 1)
var rightAnswerPlacement:UInt32 = 0
var seconds = 30
var timer = Timer()
var score = 0
if (sender.tag == Int(rightAnswerPlacement)) {
score += 1
scoreLabel.text = "\(score)"
seconds = 100
currentQuestion = Int(arc4random_uniform(32) + 1)
} else {
timer.invalidate()
end()
}
最佳答案
var currentQuestion = Int(arc4random_uniform(36) + 1)
var rightAnswerPlacement:UInt32 = 0
var seconds = 30
var timer = Timer()
var score = 0
var asked = [Int]()
var recordData:String!
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var buttonOne: UIButton!
@IBOutlet weak var buttonTwo: UIButton!
@IBOutlet weak var buttonThree: UIButton!
@IBAction func answerButtons(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerPlacement)) {
score += 1
scoreLabel.text = "\(score)"
seconds = 100
currentQuestion = Int(arc4random_uniform(36) + 1)
while asked.contains(currentQuestion) {
currentQuestion = Int(arc4random_uniform(36) + 1)
}
asked.append(currentQuestion)
print(asked)
if asked.count == questions.count {
currentQuestion = 0
return
}
关于ios - 如何跟踪数组中的哪些项目已经存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54410125/