Swift 3 Trivia app重复的答案

时间:2016-12-20 02:31:50

标签: ios swift3

the Problem

这是我的代码,正确答案将其在按钮数量中的位置随机化。但是,当前的代码似乎重复了第4个答案

它针对每个问题及其各自的答案执行此操作。

按钮标签设置简单,第一个标记为“1”,第二个标记标记为“2”,依此类推。

如果需要更多信息,请随时询问。

我已经离开了问题代码,以便其他有类似问题的人能够找到我做错的基础。

问题代码:

//
//  AnimalViewController.swift
//  It's Trival
//
//  Created by Chris Levely on 12/18/16.
//  Copyright © 2016 ZenithNomad. All rights reserved.
//

import UIKit

class AnimalViewController: UIViewController {

    let questions = ["What is the fastest fish in the sea?", "Which animal has the most legs?"]
    let answers = [["Sailfish", "Tuna", "Swordfish", "Marlin"], ["Millipede", "Spider", "Ant", "Octopus"]]

    var currentQuestion = 0
    var rightAnswerPlacement : UInt32 = 0

    @IBOutlet weak var Question: UILabel!

    @IBAction func AnswerQuestion(_ sender: AnyObject)
    {
        if (sender.tag == Int(rightAnswerPlacement))
        {
            print("Right")
        }
        else
        {
            print("Wrong")
        }

        if (currentQuestion != questions.count)
        {
            newQuestion()
        }
    }

    func newQuestion()
    {
        Question.text = questions[currentQuestion]

        rightAnswerPlacement = arc4random_uniform(4)+1

        var button : UIButton = UIButton()

        var x = 1

        for i in 1...4
        {
            button = view.viewWithTag(i) as! UIButton

            if (i == Int(rightAnswerPlacement))
            {
                button.setTitle(answers[currentQuestion][0], for: .normal)
            }
            else
            {
                button.setTitle(answers[currentQuestion][x], for: .normal)
                x = 3
            }


        }
        currentQuestion += 1
    }

    override func viewDidAppear(_ animated: Bool) {
        newQuestion()
    }

}

新固定代码:

    //
//  AnimalViewController.swift
//  It's Trival
//
//  Created by Chris Levely on 12/18/16.
//  Copyright © 2016 ZenithNomad. All rights reserved.
//

import UIKit

class AnimalViewController: UIViewController {

    let questions = ["What is the fastest fish in the sea?", "Which animal has the most legs?"]
    let answers = [["Sailfish", "Tuna", "Swordfish", "Marlin"], ["Millipede", "Spider", "Ant", "Octopus"]]

    var currentQuestion = 0
    var rightAnswerPlacement : UInt32 = 0

    @IBOutlet weak var Question: UILabel!

    @IBAction func AnswerQuestion(_ sender: AnyObject)
    {
        if (sender.tag == Int(rightAnswerPlacement))
        {
            print("Right")
        }
        else
        {
            print("Wrong")
        }

        if (currentQuestion != questions.count)
        {
            newQuestion()
        }
    }

    func newQuestion()
    {
        Question.text = questions[currentQuestion]

        rightAnswerPlacement = arc4random_uniform(4)+1

        var button : UIButton = UIButton()

        var x = 1

        for i in 1...4
        {
            button = view.viewWithTag(i) as! UIButton

            if (i == Int(rightAnswerPlacement))
            {
                button.setTitle(answers[currentQuestion][0], for: .normal)
            }
            else
            {
                button.setTitle(answers[currentQuestion][x], for: .normal)
                x += 1
            }


        }
        currentQuestion += 1
    }

    override func viewDidAppear(_ animated: Bool) {
        newQuestion()
    }

}

3 个答案:

答案 0 :(得分:2)

这是因为你硬编码x = 3,你也应该随机化x。

这是我的方法,我使用UILabel测试它,但它是相同的概念

func newQuestion()
{
    questionLabel.text = questions[currentQuestion]
    var label : UILabel = UILabel()
    var xArray = [0, 1, 2, 3]
    var x = Int(arc4random_uniform(UInt32(xArray.count)))

    for i in 1...4
    {

        label = view.viewWithTag(i) as! UILabel
        label.text = answers[currentQuestion][xArray[x]]

        if answers[currentQuestion][xArray[x]] == answers[currentQuestion][0]
        {
            print(answers[currentQuestion][xArray[x]])
            //this is the answer, do something when user click this button
        }

        //remove the index x because you don't want to get a same number next time
        xArray.remove(at: x)
        x = Int(arc4random_uniform(UInt32(xArray.count)))

    }
    currentQuestion += 1
}

enter image description here

答案 1 :(得分:0)

想象一下,在你设置一个随机按钮的正确答案的标题之前,你首先按照可能答案数组的相同顺序设置所有按钮的标题,没有问题,现在你设置了正确的字符串回答一个随机按钮,如果这个随机位置与数组中的正确答案的位置不同(0),你最终会得到一个重复的字符串,这就是实际发生的事情。

你需要做的是将位置0(正确答案)的答案换成阵列中随机位置的另一个答案,或者甚至更好地改变答案阵列,如何?我认为没有内置函数,但您可以创建一个新的空数组并从原始数组中删除一个随机答案并将其添加到新数组中,直到所有答案从原始数组切换到新数组。随机顺序。

希望它有所帮助。

答案 2 :(得分:0)

我的解决方案比随机化x值更简单一些。在上面的示例代码中,我把它写成x = 1开始,然后在newQuestion函数中写为x = 3,

我已经使用的解决方案,我现在将其作为x + = 1,因此它现在通过每个循环的数组而不是坚持数组中的第4个答案。可能不是最优雅的解决方案,但这只是这个简单的应用程序所需的一切