我在做一個測驗遊戲。我在交換機的每個「案例」中預訂了所有的問題和答案。我想要按隨機順序選擇案例,但在人回答問題並按下「下一步」按鈕後,函數「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
}
你的問題是什麼?你的代碼不工作嗎? – Qbyte