I am creating a hangman app and the if statement is not working when I remove it the code works, but only for one animal. The code should update the label, placing the letter you pressed in the appropriate place if the word contains that letter. I tried creating a switch
(random is the word that is selected from an array of animals)
switch random {
case "aardvark":
(the code for when random is aardvark)
default:
(the code for sea turtle) }
but the default case would execute every time, even when random was aardvark
@IBAction func aPressed(_ sender: Any) {
if random == "aardvark" {
if aardvark[0] == "a" {
aar[0] = "a"
};if aardvark[1] == "a" {
aar[1] = "a"
};if aardvark[2] == "a" {
aar[2] = "a"
};if aardvark[3] == "a" {
aar[3] = "a"
};if aardvark[4] == "a" {
aar[4] = "a"
};if aardvark[5] == "a" {
aar[5] = "a"
};if aardvark[6] == "a" {
aar[6] = "a"
};if aardvark[7] == "a" {
aar[7] = "a"
} else if aardvark[0] != "a" , aardvark[1] != "a" , aardvark[2] != "a" , aardvark[3] != "a" , aardvark[4] != "a" , aardvark[5] != "a" , aardvark[6] != "a" , aardvark[7] != "a" {
wrong += 1
}
theWord.text = self.aar.joined(separator: " ")
}
if random == "sea turtle" {
if seaTurtle[2] == "a" {
sTurt[2] = "a"
theWord.text = self.sTurt.joined(separator: " ")
}
}
buttonA.isHidden = true
updateImage()
}
Aucun commentaire:
Enregistrer un commentaire