2016-09-25 80 views
0

我在Xcode中創建一個測驗遊戲,測驗完成後顯示一個屏幕,顯示5個問題(來自70+的數組)正確答案(答案)和用戶選擇的答案(UserAnswer)。在一組按鈕中打印一個整數數組,Swift

我目前擁有正確答案的標籤集合(CorrectAnswer),以及用戶答案YourAnswer的單獨標籤集合。我想連續打印5個標籤(QuestionName,YourAnswer,CorrectAnswer)的每個集合中爲每個數組存儲的所有5個值。

我一般都是編碼的新手,所以在循環中使用數組的幫助是值得讚賞的。

第一個視圖控制器:

struct Question { 
    var Question : String 
    var Answers : [String]! 
    var Answer : Int! 
    var UserAnswer : Int! 
    } 

var Questions = [Question]() 

第二個視圖控制器:

class ScoreViewController: UIViewController { 

var Questions = [Question]() 
var CorrectAnswerTotal = Int() 
var QuestionNumberTotal = Int() 
let TOTAL_QUESTIONS = 0 
var AnswerNumber = Int() 


@IBOutlet var QuestionName: [UILabel]! 
@IBOutlet var YourAnswer: [UILabel]! 
@IBOutlet var CorrectAnswer: [UILabel]! 


    for i in 0..<Questions.count { 

    var q = Questions[i].Question 
    var ai = Questions[i].Answer 
    var a = Questions[i].Answers[ai] 
    var uai = Questions[i].UserAnswer 
    var ua = Questions[i].Answers[uai] 

    //This is where the error is 

    if QuestionNumberTotal < TOTAL_QUESTIONS { 

      if (ai != uai){ 

       QuestionName.text = Questions[i].Question 
       YourAnswer.text = Questions[i].UserAnswer 
       CorrectAnswer.text = Questions[i].Answer 

       } 

回答

0

隨意問是否有什麼東西不清楚。

struct Question { 
    var question: String 
    var answers: [String] 
    var correctAnswer: Int 
    var userAnswer: Int 
} 

var questions = [Question]() 
var correctAnswerTotal = 0 

@IBOutlet var questionName: [UILabel]! 
@IBOutlet var yourAnswer: [UILabel]! 
@IBOutlet var correctAnswer: [UILabel]! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // fill it with some examples 
    questions.append(Question(question: "Question 1?", answers: ["Answer 1", "Answer 2", "Answer 3", "4", "Answer 5"], correctAnswer: 0, userAnswer: 1)) 
    questions.append(Question(question: "Question 2?", answers: ["Answer 1", "Answer 2", "Answer 3", "4", "Answer 5"], correctAnswer: 1, userAnswer: 2)) 
    questions.append(Question(question: "Question 3?", answers: ["Answer 1", "Answer 2", "Answer 3", "4", "Answer 5"], correctAnswer: 3, userAnswer: 3)) 

    // assure, that the necessary amount of labels exists 
    guard questionName.count >= questions.count 
     && yourAnswer.count >= questions.count 
     && correctAnswer.count >= questions.count else { 
     print("Not enough labels.") 
     return 
    } 

    // starts at 0 and ends at last-1 
    for i in 0..<questions.count { 

     let q = questions[i] 
     let user = q.answers[q.userAnswer] 
     let correct = q.answers[q.correctAnswer] 

     // set labels texts 
     questionName[i].text = q.question 
     yourAnswer[i].text = user 
     correctAnswer[i].text = correct 

     // correct answer is green, incorrect answer is red 
     if q.correctAnswer == q.userAnswer { 
      correctAnswerTotal += 1 
      yourAnswer[i].textColor = UIColor.green 
     } else { 
      yourAnswer[i].textColor = UIColor.red 
     } 
    } 
} 
+0

謝謝這是非常有用的,但它並不是所有的工作,因爲我的標籤是出口集合不只是單一的網點。 – EllaHiggins

+0

我明白了。嘗試這個? – RyuX51

+0

感謝您的幫助,現在所有的工作 – EllaHiggins