测验应用有了不重复的问题数组 [英] Quiz App With Array without repeating questions

查看:127
本文介绍了测验应用有了不重复的问题数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能是一个更好的方式来这个应用程序,而无需重复code像我一样

我想生成使得它从一个数组重复一个不同的问题。但每一次的动作NextQuestion是pressing几次它给了我同样的问题几次,然后它去到下一个,或其他时间只preSS的人则进入到下一个。我知道这是最好的code,但我仍然在学习。

 进口的UIKit类TestQuestionsVC:UIViewController的{    @IBOutlet弱VAR SignImage:UIImageView的!    @IBOutlet弱VAR AnswerBtn_A:MaterialButton!    @IBOutlet弱VAR AnswerBtn_B:MaterialButton!    @IBOutlet弱VAR AnswerBtn_C:MaterialButton!    @IBOutlet弱VAR AnswerBtn_D:MaterialButton!    VAR questionModel = QuestionModel()    变种GuessQuestionNum =
    VAR答案:字符串=
    变种btnLatter =
    VAR previousNumber:UInt32的? //在randomNumber使用()    覆盖FUNC viewDidLoad中(){
        super.viewDidLoad()
            对于在_ 1 ... 5 {
                        randomQuestion(queModel:questionModel)            }
    }
    FUNC randomQuestion(queModel queModel:QuestionModel){        让问题= queModel.questions
        让randomIndex = INT(arc4random_uniform(UInt32的(questions.count)))        答案=\\(问题[randomIndex] .answers [问题[randomIndex] .correctAnswerIndex])        SignImage.image =问题[randomIndex]。提问
        AnswerBtn_A.setTitle(\\(问题[randomIndex] .answers [0]),forState:。正常)
        AnswerBtn_B.setTitle(\\(问题[randomIndex] .answers [1]),forState:。正常)
        AnswerBtn_C.setTitle(\\(问题[randomIndex] .answers [2]),forState:。正常)
        AnswerBtn_D.setTitle(\\(问题[randomIndex] .answers [3]),forState:。正常)        }
    覆盖FUNC didReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        可以重新创建任何资源的处置//。
    }    @IBAction FUNC ActBtn_A(发件人:AnyObject){        GuessQuestionNum =(AnswerBtn_A.titleLabel?的.text)!
        如果GuessQuestionNum == {答案
            AnswerBtn_A.backgroundColor = GREEN_COLOR
        }其他{
            AnswerBtn_A.backgroundColor = RED_COLOR        }            }    @IBAction FUNC ActBtn_B(发件人:AnyObject){
        GuessQuestionNum =(AnswerBtn_B.titleLabel?的.text)!
        如果GuessQuestionNum == {答案
            AnswerBtn_B.backgroundColor = GREEN_COLOR
        }其他{
        AnswerBtn_B.backgroundColor = RED_COLOR
        }
    }    @IBAction FUNC ActBtn_C(发件人:AnyObject){
        GuessQuestionNum =(AnswerBtn_C.titleLabel?的.text)!        如果GuessQuestionNum == {答案
            AnswerBtn_C.backgroundColor = GREEN_COLOR
        }其他{
            AnswerBtn_C.backgroundColor = RED_COLOR
        }
    }    @IBAction FUNC ActBtn_D(发件人:AnyObject){
        GuessQuestionNum =(AnswerBtn_D.titleLabel?的.text)!        如果GuessQuestionNum == {答案
            AnswerBtn_D.backgroundColor = GREEN_COLOR
        }其他{
            AnswerBtn_D.backgroundColor = RED_COLOR
        }
    }    @IBAction FUNC NextQuestion(发件人:AnyObject){        AnswerBtn_A.backgroundColor = BLUE_COLOR
        AnswerBtn_B.backgroundColor = BLUE_COLOR
        AnswerBtn_C.backgroundColor = BLUE_COLOR
        AnswerBtn_D.backgroundColor = BLUE_COLOR        对于在_ 1 ... 5 {
            randomQuestion(queModel:questionModel)        }
    }
    FUNC randomQuestion(queModel:QuestionModel){        让问题= queModel.questions        FUNC randomNumber() - > UInt32的{
            VAR randomNumber = arc4random_uniform(UInt32的(questions.count))
            而previousNumber == {randomNumber
                randomNumber = arc4random_uniform(UInt32的(questions.count))
            }
            previousNumber = randomNumber
            返回randomNumber
        }
        让randomIndex = INT((UInt32的(randomNumber())))        答案=\\(问题[randomIndex] .answers [问题[randomIndex] .correctAnswerIndex])
        SignImage.image =问题[randomIndex]。提问
        AnswerBtn_A.setTitle(\\(问题[randomIndex] .answers [0]),forState:。正常)
        AnswerBtn_B.setTitle(\\(问题[randomIndex] .answers [1]),forState:。正常)
        AnswerBtn_C.setTitle(\\(问题[randomIndex] .answers [2]),forState:。正常)
        AnswerBtn_D.setTitle(\\(问题[randomIndex] .answers [3]),forState:。正常)    }}


解决方案

有很多方法来处理这​​个问题。我的建议是建立问题索引的数组。随机指标的数组中删除一个对象,并用它来获取一个问题。示例code:

 进口基金会
让数组= [一,二,三,四有,五,六个一,七,八,九,十]
VAR指标= [INT]();FUNC randomItem() - >串
{
  如果indexes.count == 0
  {
    打印(填充索引阵)
    指数=(0 ... array.count-1).MAP {$ 0}
  }
  让randomIndex = INT(arc4random_uniform(UInt32的(indexes.count)))
  让anIndex处= indexes.removeAtIndex(randomIndex)
  返回数组[anIndex处]
}因为我在1 ... 20
{
  打印(随机字= \\(randomItem()))
}

该randomItem()函数将被写入重新填写索引数组,一旦它是空的,所以你得到一组新的随机问题。

请注意,这是可能的右后重新填充所述阵列,因为新的阵列将包含所有可能的索引,包括所使用的最后一个,以获得同样的问题在连续两次。你需要额外的逻辑添加到prevent这种情况下。

There is probably a better way to this app without repeating the code like I did

I'm trying to generate a different question with making it repeat from an array. But every time the action NextQuestion is pressing several times it gives me the same question several times then it goes to the next one, or other time it only press ones then it goes to the next. I know this is the best code but I'm still learning.

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)

    }

}

解决方案

There's lots of ways to handle this. My suggestion would be to create an array of question indexes. Randomly remove an object from the array of indexes, and use it to fetch a question. Sample code:

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())")
}

The randomItem() function is written to re-fill the indexes array once it's empty, so you get a new set of random questions.

Note that it's possible to get the same question twice in a row right after re-filling the array because the new array will contain all possible indexes, including the last one used. You'd need to add extra logic to prevent that case.

这篇关于测验应用有了不重复的问题数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆