我正在制作一个测验应用程序,每次用户按下按钮1或按钮2时,变量" currentQuestion"和" currentAnswer"增加1,在字典中给出下一个问题。
除非我运行我的应用程序并使用标签测试计数器,否则当我选择正确的答案时,它有时会加倍。我花了很长时间试图解决这个问题,但我无法理解!
我也理解我的代码可能很糟糕,如何改进它的任何建议都会很棒!
由于
import UIKit
class ViewController: UIViewController {
// Program Outlets
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var countLabel: UILabel!
@IBOutlet weak var incorrectLabel: UILabel!
@IBOutlet weak var testLabel: UILabel!
// Program Variables
var questionStorage =
[
"1" : "Red is a four letter word. True or False?" ,
"2" : "Cat is a three letter word. True or False?",
"3" : "Dog is a five letter word. True or False",
"4" : "I Smell Like a wet noodle!",
"5" : "The ultimate smell awaits"
]
// True = 1 & False = 0
var questionAnswers =
[
"1" : "0",
"2" : "1",
"3" : "0",
"4" : "1",
"5" : "0"
]
var incorrectQuestions: [String] = []
var incorrectQuestionCount = 0
var questionCount = 1
var currentQuestion = 1
var currentAnswer = 1
// Program Functions
// Question Generation Function
func Questions() {
var currentQuestionString = String(currentQuestion)
var currentAnswerString = String(currentAnswer)
// Question Selection Variables
var question = questionStorage[currentQuestionString]
var answer = questionAnswers[currentAnswerString]
// Question Counter Variables
//countLabel.text = currentQuestionString
// Question Generation Code
questionLabel.text = question
countLabel.text = answer
testLabel.text = currentAnswerString
}
// Check for quiz completion
func checkFinish() {
if questionCount == 5 {
questionLabel.text = "Test Finished"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Button Setup
button1.layer.cornerRadius = 30;
button1.layer.borderWidth = 1
button1.layer.borderColor = UIColor.blackColor().CGColor
button2.layer.cornerRadius = 30;
button2.layer.borderWidth = 1
button2.layer.borderColor = UIColor.blackColor().CGColor
// Begin Program
Questions()
}
// True & False Button Actions
@IBAction func button1Press(sender: AnyObject) {
if countLabel.text == "1" { // correct
incorrectQuestions.append(questionLabel.text!)
++currentQuestion
++currentAnswer
Questions()
checkFinish()
++questionCount
++incorrectQuestionCount
}
if countLabel.text == "0" { // incorrect
incorrectQuestions.append(questionLabel.text!)
++currentQuestion
++currentAnswer
Questions()
checkFinish()
++questionCount
++incorrectQuestionCount
}
}
@IBAction func button2Press(sender: AnyObject) {
if countLabel.text == "0" { // correct
incorrectQuestions.append(questionLabel.text!)
++currentQuestion
++currentAnswer
Questions()
checkFinish()
++questionCount
++incorrectQuestionCount
}
if countLabel.text == "1" { // incorrect
incorrectQuestions.append(questionLabel.text!)
++currentQuestion
++currentAnswer
Questions()
checkFinish()
++questionCount
++incorrectQuestionCount
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
在button1Press
中,
if countLabel.text == "1"
和
if countLabel.text == "0"
对Questions
的调用可能会更改countLabel.text
的值
它设置为"0"
,您将执行第二个条件以及第一个条件。
将if countLabel.text == "0"
替换为
else
与button2Press
同样如此。