我正在设计一个小型游戏,并且已经确定了一种初始方法,该方法是根据询问问题和按下输入按钮之间经过的秒数来分配分数。
我知道最佳实践是创建一个要在要重复的行时调用的函数,但是在我看来,当只有两行重复四次时调用它可能有点多余。使用下面的代码,为每个部分的最后两行创建一个函数对我来说是“适当”还是更优选?
//Assigns points to score based on elapsed time and updates score
func updateScore() {
timer.invalidate()
if timeToAnswer < 3 {
questionScore = 10 //10 points for answering so quickly
score = score + questionScore //Updates score
timeToAnswer = 0 //Resets var timeToAnswer
}
else if (timeToAnswer >= 3) && (timeToAnswer < 6) {
questionScore = 7
score = score + questionScore
timeToAnswer = 0
}
else if (timeToAnswer >= 6) && (timeToAnswer < 11) {
questionScore = 10 - timeToAnswer
score = score + questionScore
timeToAnswer = 0
}
else {
questionScore = 1
score = score + questionScore
timeToAnswer = 0
}
}
非常感谢任何提供帮助或建议的人。
答案 0 :(得分:0)
您可以采用更好的方式进行操作。 您也可以使用switch语句。无论如何:
func updateScore() {
timer.invalidate()
if timeToAnswer < 3 {
questionScore = 10
}
else if (timeToAnswer >= 3) && (timeToAnswer < 6) {
questionScore = 7
}
else if (timeToAnswer >= 6) && (timeToAnswer < 11) {
questionScore = 10 - timeToAnswer
}
else {
questionScore = 1
}
score = score + questionScore //Updates score
timeToAnswer = 0 //Resets var timeToAnswer
}