尝试在 Xcode 7 (Swift 2) 中为 iOS 移动应用编写验证循环 - 陷入无限循环
Trying to write a validation loop in Xcode 7 (Swift 2) for an iOS mobile app - stuck on infinite loop
我正在尝试创建一个应用程序 (iOS),其中使用随机数生成器来显示算术问题。随机数生成器将决定用户将执行什么操作(乘法、减法、加法)以及使用哪些操作数。用户可以使用分段控件 select 难度级别。
用户可以尝试 3 次来提供正确答案。用户将他们的答案输入文本字段,然后按下按钮提交他们的答案。如果用户在第三次尝试后仍未正确回答,程序将显示正确答案并显示新问题。
我在编写代码时遇到困难,无法为用户提供三种尝试。截至目前,我已经成功地创建了一个无限循环。下面是我的部分代码(注意:int i 目前是一个全局变量):
@IBAction func submitAnswer(sender: UIButton) {
var newAnswer=0
if enteredAnswer.text!.isEmpty {
let alert = UIAlertController(title: "Warning!", message: "Please enter an answer.", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
else {
let textAnswer: String = enteredAnswer.text!
let userAnswer = Int(textAnswer)!
if correctAnswer != userAnswer && i < 3{
newAnswer = userAnswer
while newAnswer != correctAnswer {
feedback.text = "Try Again!"
enteredAnswer.text = ""
newAnswer = Int(textAnswer)!
i+=1
}
}
if correctAnswer == userAnswer {
feedback.text = "Correct Answer!"
enteredAnswer.text = ""
score += 1
scoreValue.text = String(score)
i=0
setProblem()
}
else {
feedback.text = "The correct answer \n in \n \(correctAnswer)!"
enteredAnswer.text = ""
score -= 1
scoreValue.text = String(score)
i=0
setProblem()
}
}
}
如果只想中断 while 循环:
while newAnswer != correctAnswer {
feedback.text = "Try Again!"
enteredAnswer.text = ""
newAnswer = Int(textAnswer)!
i+=1
if i == 3 {
break
}
}
如果你想让你的游戏正常运行:
if newAnswer != correctAnswer {
feedback.text = "Try Again!"
enteredAnswer.text = ""
newAnswer = Int(textAnswer)!
i+=1
if (i<3){
return
}
}
我正在尝试创建一个应用程序 (iOS),其中使用随机数生成器来显示算术问题。随机数生成器将决定用户将执行什么操作(乘法、减法、加法)以及使用哪些操作数。用户可以使用分段控件 select 难度级别。
用户可以尝试 3 次来提供正确答案。用户将他们的答案输入文本字段,然后按下按钮提交他们的答案。如果用户在第三次尝试后仍未正确回答,程序将显示正确答案并显示新问题。
我在编写代码时遇到困难,无法为用户提供三种尝试。截至目前,我已经成功地创建了一个无限循环。下面是我的部分代码(注意:int i 目前是一个全局变量):
@IBAction func submitAnswer(sender: UIButton) {
var newAnswer=0
if enteredAnswer.text!.isEmpty {
let alert = UIAlertController(title: "Warning!", message: "Please enter an answer.", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
else {
let textAnswer: String = enteredAnswer.text!
let userAnswer = Int(textAnswer)!
if correctAnswer != userAnswer && i < 3{
newAnswer = userAnswer
while newAnswer != correctAnswer {
feedback.text = "Try Again!"
enteredAnswer.text = ""
newAnswer = Int(textAnswer)!
i+=1
}
}
if correctAnswer == userAnswer {
feedback.text = "Correct Answer!"
enteredAnswer.text = ""
score += 1
scoreValue.text = String(score)
i=0
setProblem()
}
else {
feedback.text = "The correct answer \n in \n \(correctAnswer)!"
enteredAnswer.text = ""
score -= 1
scoreValue.text = String(score)
i=0
setProblem()
}
}
}
如果只想中断 while 循环:
while newAnswer != correctAnswer {
feedback.text = "Try Again!"
enteredAnswer.text = ""
newAnswer = Int(textAnswer)!
i+=1
if i == 3 {
break
}
}
如果你想让你的游戏正常运行:
if newAnswer != correctAnswer {
feedback.text = "Try Again!"
enteredAnswer.text = ""
newAnswer = Int(textAnswer)!
i+=1
if (i<3){
return
}
}