如何不重复 Switch 的情况? (Swift)
How not repeat Switch cases? (Swift)
我在做问答游戏。我在每个"case"的开关中预订了所有的问题和答案。我希望以随机顺序选择案例,但在该人回答问题并按下按钮 "next" 后,功能 "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
}
您必须将变量 previousNumber
作为全局(实例)变量放在方法之外
var previousNumber = UInt32.max // declare the variable with an "impossible" value
func randomQuestions()
{
var randomNumber : UInt32
do {
randomNumber = arc4random_uniform(3)
} while previousNumber == randomNumber
previousNumber = randomNumber
switch(randomNumber) {
...
一种避免多次出现相同随机数的方法,您可以创建一个包含问题编号的数组,然后将其随机排列。
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]
提出问题,依此类推。
我在做问答游戏。我在每个"case"的开关中预订了所有的问题和答案。我希望以随机顺序选择案例,但在该人回答问题并按下按钮 "next" 后,功能 "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
}
您必须将变量 previousNumber
作为全局(实例)变量放在方法之外
var previousNumber = UInt32.max // declare the variable with an "impossible" value
func randomQuestions()
{
var randomNumber : UInt32
do {
randomNumber = arc4random_uniform(3)
} while previousNumber == randomNumber
previousNumber = randomNumber
switch(randomNumber) {
...
一种避免多次出现相同随机数的方法,您可以创建一个包含问题编号的数组,然后将其随机排列。
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]
提出问题,依此类推。