如何在选择数组中的正确答案后显示消息?

时间:2017-12-24 13:19:56

标签: swift

所以这是我用问题和答案使用数组的程序。数组中的第一个答案始终是正确的,但它在实际应用程序中随机化。

我想知道如何做一些事情,如果有人得到正确的答案,屏幕渐变成不同的颜色并告诉他们是否正确,并说例如“是德国赢得了最多德比“在屏幕上显示它几秒钟,游戏继续进行。

对不起,如果这有点太多了,但如果你能提供帮助,我真的很感激。

0

1 个答案:

答案 0 :(得分:1)

有更好的方法来做你正在做的事情,但我希望这会有所帮助,并给你一些指示。所有最好的和快乐的圣诞节!

let questions = ["Who's won the most England-Germany derbies in the world cup?", "Which match has had the most cards in the world cup?", "Who has done the best in the world cup"]

let answers = [["Germany", "England"], ["Portugal vs Netherlands", "England vs Spain"], ["Lionel Messi", "Christiano Ronaldo"]]

var questionNumber: Int = 0 // first question
var correctAnswer: Int = 0 // choose correct answer
var points: Int = 0

func handleAnswerChosen(_ sender: UIButton) {
    if sender.tag == 0 { // make sure that your number tags are always 0 and 1. 0 for the first answer, 1 for the second.
        if questionNumber == 0 && sender.tag == correctAnswer {
            print("CORRECT")

// MARK: SHOW CORRECT ANSWER AND SELECTED ANSWER.

            print(answers[questionNumber][correctAnswer]) // CORRECT ANSWER
            print(answers[questionNumber][sender.tag]) // ANSWER SELECTED

            handleNextQuestion()
        }
    }
}

func handleNextQuestion() {
    questionNumber += 1 // Do this as well as update to the next question.

    if questionNumber == 0 { // first question
        correctAnswer = 1 // second answer
    } else if questionNumber == 1 {
        correctAnswer = 0 // first answer
    }

    performSegue(withIdentifier: "showScore", sender: self) 
}

// The first [0] gets the two possible answers. The second [1] gets the second answer of the two possible answers chosen.
print(answers[0][1]) // this prints England which is the first question and the second answer of the first question