2015-04-22 14 views
-1

我有一个类似测验的应用程序。它经常出现所有问题。我如何让他们只出现一次,所以在最后一个问题后,会弹出一条消息?测验显示相同的问题

func nextText(){ 
     let randomNumber = Int(arc4random_uniform(4)) 
     var textLabel = "" as NSString 
     switch (randomNumber){ 
     case 1: 
      textLabel = "Question1." 
      break 
     case 2: 
      textLabel = "Question2." 
      break 
     case 3: 
      textLabel = "Question3." 
      break 
     case 4: 
      textLabel = "Question4." 
      break 
     default: 
      textLabel = "Question 5" 
     } 
     self.textLabel.text = textLabel as? String 
    } 
} 

回答

0

只要把不同的情况下,内部:

func nextText(){ 
     let randomNumber = Int(arc4random_uniform(4)) 
     var textLabel = "" as NSString 
     switch (randomNumber){ 
     case 1: 
      textLabel = "Question1." 
      self.textLabel.text = textLabel as? String 
      break 
     case 2: 
      textLabel = "Question2." 
      self.textLabel.text = textLabel as? String 
      break 
     case 3: 
      textLabel = "Question3." 
      self.textLabel.text = textLabel as? String 
      break 
     case 4: 
      textLabel = "Question4." 
      self.textLabel.text = textLabel as? String 
      break 
     default: 
      textLabel = "Question 5" 
      self.textLabel.text = textLabel as? String 
     } 
    } 
+0

你所做的只是改变你设置'textLabel.text'的地方,这段代码和问题中的代码结果完全一样。 – ABakerSmith

+0

@ABakerSmith我收到了所有不同的人。 –

+0

我认为问题是要求不要重复的问题。除非我误解。 – ABakerSmith

0

也许你可以存储问题作为一个数组。然后创建了一系列混合问题。然后运行向用户展示问题的阵列。一旦你到了阵列中的最后一个问题,你可以显示你的弹出消息。这里有一个例子:

洗牌阵列,从How do I shuffle an array in Swift?

extension Array { 
    func shuffled() -> [T] { 
     var list = self 
     for i in 0..<(list.count - 1) { 
      let j = Int(arc4random_uniform(UInt32(list.count - i))) + i 
      swap(&list[i], &list[j]) 
     } 
     return list 
    } 
} 

2.您的问题:

let questions = ["Question1", "Question2", "Question3", "Question4"] 
let shuffledQuestions = questions.shuffled() 

枚举通过你的洗牌问题:

var questionIndex = 0 

for question in shuffledQuestions { 
    // Present the question to the user 
    self.textLabel.text = question 
    questionIndex++ 
} 

4.一旦用户到达最后一个问题出现弹出窗口。例如。当questionIndex == shuffledQuestions.count

0

定义一个可变数组。将randomNumber添加到数组中。检查randomNumber是否存在于数组中。如果它存在重现一个随机数。

这是你的问题在Objective - C:

//Defining an array 
@property NSMutableArray *asked; 


-(void) nextText{ 
//This will generate a random number between 1 and 5. 
int randomNumber = arc4random() % 5+1; 

NSString * textLabel =nil; 

//This will check all questions are asked 
if ([asked count] ==5) { 
    self.myLabel.text = @"All questions completed"; 
    return; 
} 

//This will check is random number asked 
if ([asked containsObject:[NSString stringWithFormat:@"%i",randomNumber]]) { 
    return; 
} 

//This will add random number to asked questions and will ask the question of random number 
else{ 
    [asked addObject:[NSString stringWithFormat:@"%i",randomNumber]]; 
    switch (randomNumber){ 
     case 1: 
      textLabel = @"Question1."; 
      break; 
     case 2: 
      textLabel = @"Question2."; 
      break; 
     case 3: 
      textLabel = @"Question3."; 
      break; 
     case 4: 
      textLabel = @"Question4."; 
      break; 
     case 5: 
      textLabel = @"Question5."; 
      break; 
    } 
    self.myLabel.text = textLabel; 
    } 
} 

//If you want more than limited question, create and fill question array 
//After you can ask like this. Change else part with this 
else{ 
    [asked addObject:[NSString stringWithFormat:@"%i",randomNumber]]; 
    self.myLabel.text = [questions objectAtIndex:randomNumber]; 
    } 
+0

这种方法的问题是,当您向数组添加更多数字时,选择未选择数字的概率会降低。只有五个问题时,这并不是什么大问题。但是,如果还有更多,这可能最终需要花费相当长的时间才能为问题订单。 – ABakerSmith

+0

如果你不想问只有5个问题,你可以为问题创建一个更多的数组。并将开关部分置于循环中。 like: //这会将随机数字添加到提问中,并会询问随机数 的问题else { [addObject:[NSString stringWithFormat:@“%i”,randomNumber]]; self.myLabel.text = [questions objectAtIndex:randomNumber]; } – kordiseps

0

你为什么不干脆把问题一个数组,并从中弹出每次你展示一个问题吗?所以你可以做这样的事情:

var questions = ["Question1", "Question2", "Question3", "Question4", "Question5"] 

for var i = questions.count; i > 0; { 
    let randomNumber = Int(arc4random_uniform(UInt32(--i))) 
    let textLabel = questions[randomNumber] as String 
    println(textLabel) 
    questions.removeAtIndex(randomNumber) 
} 

if questions.count == 0 { 
    println("should show the popup") 
}