Ios Swift 3琐事应用程序重复答案
这是我的代码,正确的答案将其在按钮数量中的位置随机化。然而,当前代码似乎重复了第四个答案 它对每个问题及其相应的答案集都这样做 按钮标签设置简单,第一个按钮标签为“1”,第二个按钮标签为“2”,依此类推 如果需要更多信息,请随时询问 我留下了问题代码,这样其他有类似问题的人就有了了解我做错了什么的基础 问题代码:Ios Swift 3琐事应用程序重复答案,ios,swift3,Ios,Swift3,这是我的代码,正确的答案将其在按钮数量中的位置随机化。然而,当前代码似乎重复了第四个答案 它对每个问题及其相应的答案集都这样做 按钮标签设置简单,第一个按钮标签为“1”,第二个按钮标签为“2”,依此类推 如果需要更多信息,请随时询问 我留下了问题代码,这样其他有类似问题的人就有了了解我做错了什么的基础 问题代码: // // AnimalViewController.swift // It's Trival // // Created by Chris Levely on 12/18/16
//
// 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()
}
}
想象一下,在将正确答案的标题设置为随机按钮之前,您首先将所有按钮的标题设置为可能答案数组的相同顺序,没有问题,现在您将正确答案的字符串设置为随机按钮,如果此随机位置与数组中正确答案的位置不相同(0),您将得到一个重复的字符串,这就是实际发生的情况 您需要做的是将位于位置0(右答案)的答案切换到数组中随机位置的另一个答案,或者更好地洗牌答案数组,如何?我认为没有内置函数,但您可以创建一个新的空数组,从原始数组中删除一个随机答案并将其添加到新数组中,直到所有答案以随机顺序从原始数组切换到新数组
希望有帮助。这是因为您硬编码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
}
我的解决方案比随机分配x值要简单一些。在上面的示例代码中,我将其写成x=1开始,然后在newQuestion函数中写成x=3
我使用的解决方案,现在是x+=1,所以它现在通过每个循环的数组,而不是坚持数组中的第四个答案。可能不是最优雅的解决方案,但它是这个简单应用所需要的全部为什么
回答[currentQuestion][x]
?当硬编码的x
第一次为1时,其余时间均为3?x=1是因为我不想访问数组中的0,因为这是正确答案。只需要访问一次。所以我从1开始计算。然而,我不应该在最后硬编码x=3。只需要为每个按钮增加它,使其在字符串数组中向前移动。“我不应该硬编码x=3”,哦,好吧,这样你就得到了你自己问题的答案。:)当我试着按照你说的去做,并随机化x值。同样的,你在这里。我得到了更多的副本。除了现在,它会重复一个随机答案,有时会重复不止一次,因为您可以在您实现的新方法上更新代码吗?我已经更新了问题,以包含问题的修复方法。:)