我正在进行测验,这个问题有多个正确答案。例如,在下面的代码中,正确的答案是"粉红色"问题是"病变的颜色是什么"。但我也希望它能够认识到红色也算作正确的答案。有没有办法为一个问题编码多个正确的答案?任何帮助将不胜感激。谢谢!
import Foundation
import UIKit
class Case1Q2ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var imageButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet var questionLabel: UILabel!
@IBOutlet var correctAnswerLabel: UILabel!
@IBOutlet var inputTextField: UITextField!
var enteredAnswer: String?
var correctAnswers = ["Pink", "Red"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Case1Q2ViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Case1Q2ViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
inputTextField.delegate = self
titlesForLabels()
nextButton.enabled = false
imageButton.layer.cornerRadius = 2;
imageButton.layer.borderWidth = 2;
imageButton.layer.borderColor = UIColor.blackColor().CGColor
questionLabel.layer.cornerRadius = 2;
questionLabel.layer.borderWidth = 2;
questionLabel.layer.borderColor = UIColor.blackColor().CGColor
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func titlesForLabels() {
questionLabel.text = "What is the colour of the lesion(s)?"
correctAnswerLabel.text = correctAnswers.joinWithSeparator(", ")
correctAnswerLabel.hidden = true
inputTextField.text = nil
inputTextField.enabled = true
}
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.frame.origin.y = -keyboardFrame.size.height
})
}
func keyboardWillHide() {
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.frame.origin.y = 0
})
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
enteredAnswer = textField.text
checkForCorrectAnswer()
return true
}
func checkForCorrectAnswer() {
let answer = enteredAnswer!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
if correctAnswers.contains(answer.lowercaseString) {
print("Correct")
correctAnswerLabel.textColor = UIColor.greenColor()
correctAnswerLabel.text = "Correct!"
nextButton.enabled = true
} else {
print("Wrong Answer")
correctAnswerLabel.textColor = UIColor.redColor()
correctAnswerLabel.text = "Incorrect! Please try again"
nextButton.enabled = false
}
correctAnswerLabel.hidden = false
}
override func viewWillAppear(animated: Bool) {
navigationItem.title = "Case 1: Q2"
}
}
答案 0 :(得分:2)
您可以将var correctAnswer = "Pink"
行改为var correctAnswers = ["pink", "red"]
然后,在checkForCorrectAnswer()
方法中,您必须检查正确答案数组是否包含输入的答案:
func checkForCorrectAnswer() {
let answer = enteredAnswer!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
if correctAnswers.contains(answer.lowercaseString) {
// Correct
} else {
// Incorrect
}
}