I'm trying to create a bingo number generator app, however after generating numbers "Fatal error: Index fell out of range" was encountered randomly on line "let letter = prefixes[bingoBall/15]".
var numbersSeen = Set<Int>()
var prefixes = ["B-", "I-", "N-", "G-", "O-"]
func randomNumber() -> Int {
var nextNum : Int
repeat {
nextNum = Int.random(in: 1...75)
if !numbersSeen.contains(nextNum) {
numbersSeen.insert(nextNum)
return Int(nextNum)
}
} while true
}
func randomBall() {
let bingoBall = randomNumber()
let letter = prefixes[bingoBall/15]
lblNumber.text = "\(letter)\(bingoBall)"
lblNumCalled.text = String(numbersSeen.count) + " numbers called."
lblNumRemain.text = String(75 - numbersSeen.count) + " numbers remaining."
if lblNumPrev.text != "" {
lblNumPrev.text = lblNumPrev.text!+", "+lblNumber.text!
}else{
lblNumPrev.text = lblNumber.text!
}
}
Aucun commentaire:
Enregistrer un commentaire