gpt4 book ai didi

swift - 如何不重复Switch case? ( swift )

转载 作者:行者123 更新时间:2023-11-30 10:12:32 24 4
gpt4 key购买 nike

我正在做一个问答游戏。我记录了每个开关“案例”中的所有问题及其答案。我希望以随机顺序选择案例,但在该人回答问题并按下“下一步”按钮后,“randomQuestions”功能再次起作用,但是我不希望重复以前使用的相同案例.

func randomQuestions ()
{
var randomNumber = arc4random_uniform(3)
while previousNumber == randomNumber
{
randomNumber = arc4random_uniform(3)
}
previousNumber = randomNumber

switch(randomNumber)
{
case 0:
textoHideUnhide()
questionsLabel.text = "What color is the sun?"
button1.setTitle("Yellow", forState: UIControlState.Normal)
button2.setTitle("Black", forState: UIControlState.Normal)
button3.setTitle("Blue", forState: UIControlState.Normal)
button4.setTitle("White", forState: UIControlState.Normal)
correctAnswer = "1"
break
case 1:
textoHideUnhide()
questionsLabel.text = "What color is the moon?"
button1.setTitle("Red", forState: UIControlState.Normal)
button2.setTitle("Blue", forState: UIControlState.Normal)
button3.setTitle("White", forState: UIControlState.Normal)
button4.setTitle("Orange", forState: UIControlState.Normal)
correctAnswer = "3"
break
case 2:
textoHideUnhide()
questionsLabel.text = "What color is the grass?"
button1.setTitle("White", forState: UIControlState.Normal)
button2.setTitle("Green", forState: UIControlState.Normal)
button3.setTitle("Orange", forState: UIControlState.Normal)
button4.setTitle("Red", forState: UIControlState.Normal)
correctAnswer = "2"
break
default:
break
}

最佳答案

为了避免多次出现相同的随机数,您可以创建一个包含问题编号的数组,然后对其进行洗牌。

var indices = [0, 1, 2]

for i in 0 ..< indices.count {
var temp = indices[i]
var j = arc4random_uniform(indices.count)
indices[i] = indices[j]
indices[j] = temp
}

第一个问题是 indices[0] 中提供的编号问题,当用户单击“下一步”时,您会在 indices[1] 中提出问题,所以上。

关于swift - 如何不重复Switch case? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32191114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com