2015-04-22 33 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") 
}