Ios 计数器属性不更改值
我正在Swift游乐场应用程序中进行测验,我想保留正确答案数量的计数器。Ios 计数器属性不更改值,ios,swift,Ios,Swift,我正在Swift游乐场应用程序中进行测验,我想保留正确答案数量的计数器。correct的值没有改变,我也不知道为什么 var correct:Int = 0 func qa(question: String, answer: String) { show(question) var ans = ask("answer") if (ans.lowercased() == answer) { show("correct") corr
correct
的值没有改变,我也不知道为什么
var correct:Int = 0
func qa(question: String, answer: String) {
show(question)
var ans = ask("answer")
if (ans.lowercased() == answer) {
show("correct")
correct = correct + 1
} else {
show("wrong you numpty!")
}
}
func qaor(question: String, answer: String, answer2: String) {
show(question)
var ans = ask("answer")
if (ans.lowercased() == answer) || (ans.lowercased() == answer2) {
show("correct")
correct = correct + 1
} else {
show("wrong you numpty!")
}
}
show("What is your name?")
let name = ask("Name")
show("Hi " + name)
qa(question: "What is the name of the character played by Simon Jones in the Hitchikers Guide to the Galaxy?", answer: "arthur dent")
qaor(question: "What is voiced by Peter Jones in the Hitchikers Guide to the Galaxy?", answer: "the book", answer2: "the guide")
qa(question: "finish this sentence .doing the coastlines was always my Favourite, the rough crinkley edges, .... ", answer: "fjords")
var cf = "no"
if (correct == 0) {
var cf = "no"
} else if (correct == 1) {
var cf = "one"
} else if (correct == 2) {
var cf = "two"
} else if (correct == 3) {
var cf = "three"
}
show("you got " + cf + " questions correct out of three")
不要为每个if/else语句声明
cf
变量。它会创建一个局部变量,并且您不会更改第一个cf
相反,请尝试以下方法:
var cf = "no"
if (correct == 0) {
cf = "no"
} else if (correct == 1) {
cf = "one"
} else if (correct == 2) {
cf = "two"
} else if (correct == 3) {
cf = "three"
}
show("you got " + cf + " questions correct out of three")
不要在if-else块中重新声明
cf
。只需执行cf=“no”
等操作。请至少根据一些样式指南对代码进行格式化和缩进。这段代码不可读,不属于StackOverflow。谢谢你,berksy92,我刚刚测试了它,它工作正常