这个应用程序可能有更好的方法,而不像我那样重复代码
我正在尝试从数组中重复生成一个不同的问题。但是每当NextQuestion按下几次动作时,它会多次给我同样的问题然后它会转到下一个,或者其他时间只按下那个然后它会转到下一个。我知道这是最好的代码,但我还在学习。
import UIKit
class TestQuestionsVC: UIViewController {
@IBOutlet weak var SignImage: UIImageView!
@IBOutlet weak var AnswerBtn_A: MaterialButton!
@IBOutlet weak var AnswerBtn_B: MaterialButton!
@IBOutlet weak var AnswerBtn_C: MaterialButton!
@IBOutlet weak var AnswerBtn_D: MaterialButton!
var questionModel = QuestionModel()
var GuessQuestionNum = ""
var answer: String = ""
var btnLatter = ""
var previousNumber: UInt32? // used in randomNumber()
override func viewDidLoad() {
super.viewDidLoad()
for _ in 1...5 {
randomQuestion(queModel: questionModel)
}
}
func randomQuestion(queModel queModel:QuestionModel){
let questions = queModel.questions
let randomIndex = Int(arc4random_uniform(UInt32(questions.count)))
answer = "\(questions[randomIndex].answers[questions[randomIndex].correctAnswerIndex])"
SignImage.image = questions[randomIndex].question
AnswerBtn_A.setTitle("\(questions[randomIndex].answers[0])", forState: .Normal)
AnswerBtn_B.setTitle("\(questions[randomIndex].answers[1])", forState: .Normal)
AnswerBtn_C.setTitle("\(questions[randomIndex].answers[2])", forState: .Normal)
AnswerBtn_D.setTitle("\(questions[randomIndex].answers[3])", forState: .Normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func ActBtn_A(sender: AnyObject) {
GuessQuestionNum = (AnswerBtn_A.titleLabel?.text)!
if GuessQuestionNum == answer {
AnswerBtn_A.backgroundColor = GREEN_COLOR
} else {
AnswerBtn_A.backgroundColor = RED_COLOR
}
}
@IBAction func ActBtn_B(sender: AnyObject) {
GuessQuestionNum = (AnswerBtn_B.titleLabel?.text)!
if GuessQuestionNum == answer {
AnswerBtn_B.backgroundColor = GREEN_COLOR
} else {
AnswerBtn_B.backgroundColor = RED_COLOR
}
}
@IBAction func ActBtn_C(sender: AnyObject) {
GuessQuestionNum = (AnswerBtn_C.titleLabel?.text)!
if GuessQuestionNum == answer {
AnswerBtn_C.backgroundColor = GREEN_COLOR
} else {
AnswerBtn_C.backgroundColor = RED_COLOR
}
}
@IBAction func ActBtn_D(sender: AnyObject) {
GuessQuestionNum = (AnswerBtn_D.titleLabel?.text)!
if GuessQuestionNum == answer {
AnswerBtn_D.backgroundColor = GREEN_COLOR
} else {
AnswerBtn_D.backgroundColor = RED_COLOR
}
}
@IBAction func NextQuestion(sender: AnyObject) {
AnswerBtn_A.backgroundColor = BLUE_COLOR
AnswerBtn_B.backgroundColor = BLUE_COLOR
AnswerBtn_C.backgroundColor = BLUE_COLOR
AnswerBtn_D.backgroundColor = BLUE_COLOR
for _ in 1...5 {
randomQuestion(queModel: questionModel)
}
}
func randomQuestion( queModel:QuestionModel){
let questions = queModel.questions
func randomNumber() -> UInt32 {
var randomNumber = arc4random_uniform(UInt32(questions.count))
while previousNumber == randomNumber {
randomNumber = arc4random_uniform(UInt32(questions.count))
}
previousNumber = randomNumber
return randomNumber
}
let randomIndex = Int((UInt32(randomNumber())))
answer = "\(questions[randomIndex].answers[questions[randomIndex].correctAnswerIndex])"
SignImage.image = questions[randomIndex].question
AnswerBtn_A.setTitle("\(questions[randomIndex].answers[0])", forState: .Normal)
AnswerBtn_B.setTitle("\(questions[randomIndex].answers[1])", forState: .Normal)
AnswerBtn_C.setTitle("\(questions[randomIndex].answers[2])", forState: .Normal)
AnswerBtn_D.setTitle("\(questions[randomIndex].answers[3])", forState: .Normal)
}
}
答案 0 :(得分:0)
有很多方法可以解决这个问题。我的建议是创建一个问题索引数组。从索引数组中随机删除对象,并使用它来获取问题。示例代码:
import Foundation
let array = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
var indexes = [Int]();
func randomItem() -> String
{
if indexes.count == 0
{
print("Filling indexes array")
indexes = (0...array.count-1).map{$0}
}
let randomIndex = Int(arc4random_uniform(UInt32(indexes.count)))
let anIndex = indexes.removeAtIndex(randomIndex)
return array[anIndex];
}
for i in 1...20
{
print("random word = \(randomItem())")
}
写入randomItem()函数以在索引数组为空时重新填充它,这样您就可以获得一组新的随机问题。
请注意,重新填充数组后,可以连续两次获得相同的问题,因为新数组将包含所有可能的索引,包括最后使用的索引。您需要添加额外的逻辑来防止这种情况。