Swift 3 知识竞赛应用程序 重复答案

3

问题截图

这是我的代码,正确答案应该在按钮数量中随机分布,但是当前代码似乎会复制第四个答案。

每个问题及其相应的答案都会出现此问题。

按钮标签设置很简单,第一个按钮标记为“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()
    }

}

为什么要使用 answers[currentQuestion][x]?第一次硬编码的 x 为1,之后每次都是3? - Cœur
x=1是因为我不想访问数组中的0,因为那是正确答案,只需要访问一次。所以我将值从1开始。然而,我不应该在最后硬编码x=3,只需要为每个按钮递增它,这样它就可以在字符串数组中向前移动。 - Chris Levely
“我不应该硬编码 x = 3”,好吧,那么你已经得到了自己问题的答案。 :) - Cœur
3个回答

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


当我尝试按照你说的方式随机化x值时,我得到了更多的重复项,除了现在它会重复一个随机答案,有时甚至不止一次。 - Chris Levely
你能否更新你的代码以适应你实现的新方法? - bubibu
我已经更新了问题,包括解决我的问题的方法。 :) - Chris Levely

0
想象一下,在将正确答案的标题设置为随机按钮之前,您首先按可能答案数组的相同顺序设置所有按钮的标题,没有问题,现在您将正确答案的字符串设置为随机按钮,如果此随机位置与数组中正确答案(0)的位置不同,则会出现重复字符串,这实际上就是发生的情况。
您需要做的是将位置0(正确答案)的答案与数组中的另一个随机位置的答案交换,甚至更好的方法是打乱答案数组,如何实现呢?我认为没有内置函数,但您可以创建一个新的空数组,并从原始数组中删除一个随机答案并将其添加到新数组中,直到所有答案以随机顺序从原始数组切换到新数组中。
希望能有所帮助。

0

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

我采用的解决方案是现在将x + = 1,因此它现在通过每个循环遍历数组,而不是坚持数组中的第4个答案。可能不是最优雅的解决方案,但对于这个简单的应用程序来说,这就是所需的全部。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接