/ /条件付きステートメントの予期される宣言-swift、if-statement、conditional

条件ステートメントの予想される宣言-迅速、ifステートメント、条件

私は今、4つの選択肢のうちの1つに答えを入れるシステムをセットアップしましたが、奇妙なエラーが表示されました:

     var optionAnswer:UInt32 = arc4random_uniform(4)
if optionAnswer == 0 {                    // Expected declaration
optionA.text = secretAnsarrrrr.text
}
if optionAnswer == 1 {
optionB.text = secretAnsarrrrr.text
}
if optionAnswer == 2 {
optionC.text = secretAnsarrrrr.text
}
if optionAnswer == 3 {
optionD.text = secretAnsarrrrr.text
}

エラーは最初の条件文にのみ表示され、何をする必要があるかを指定していませんでした。これを修正するにはどうすればよいですか?

フルコード:

     import UIKit

class ViewController: UIViewController {

@IBOutlet var numA: UILabel!
@IBOutlet var operation: UILabel!
@IBOutlet var numB: UILabel!
@IBOutlet var secretAnsarrrrr: UILabel!

@IBOutlet var optionA: UILabel!
@IBOutlet var optionB: UILabel!
@IBOutlet var optionC: UILabel!
@IBOutlet var optionD: UILabel!

@IBOutlet var level: UILabel!
@IBAction func startTest(sender: UIButton) {
var question:Int = 1
func generateQuestion() {
var answer:Float = 0.0
var randomoperation:UInt32 = arc4random_uniform(4)
if randomoperation == 0 {
operation.text = "+"
}
if randomoperation == 1 {
operation.text = "-"
}
if randomoperation == 2 {
operation.text = "X"
}
if randomoperation == 3 {
operation.text = "/"
}
var randomNumber:UInt32 = arc4random_uniform(1000)
var randomNumber2:UInt32 = arc4random_uniform(1000)
// 1000 is my maximum number for now.
randomNumber += 1
randomNumber2 += 1
func identifyVal() {
if randomNumber < randomNumber2 {
var between:UInt32 = 1000 - randomNumber2
randomNumber = randomNumber2 + arc4random_uniform(between - 1)
//making sure that randomNumber is not smaller than randomNumber2, therefore all results are positive.
}
}
if operation.text == "/" {
identifyVal()

answer = round(Float(randomNumber)/Float(randomNumber2))
}

if operation.text == "+" {
answer = Float(randomNumber + randomNumber2)
}
if operation.text == "-" {
identifyVal()
answer = Float(randomNumber - randomNumber2)
}
if operation.text == "x" {
answer = Float(randomNumber * randomNumber2)
}
secretAnsarrrrr.text = "(answer)"
numA.text = String(Int(randomNumber))
numB.text = String(Int(randomNumber2))
}
generateQuestion()
}
var optionAnswer:UInt32 = arc4random_uniform(4)
if optionAnswer == 0 {
optionA.text = secretAnsarrrrr.text
}
if optionAnswer == 1 {
optionB.text = secretAnsarrrrr.text
}
if optionAnswer == 2 {
optionC.text = secretAnsarrrrr.text
}
if optionAnswer == 3 {
optionD.text = secretAnsarrrrr.text
}
var correct:Bool?
@IBAction func answerA(sender: UIButton) {
if optionAnswer == 0 {
correct = true
} else {
correct = false
}
}
@IBAction func answerB(sender: UIButton) {
if optionAnswer == 1 {
correct = true
} else {
correct = false
}
}
@IBAction func answerC(sender: UIButton) {
if optionAnswer == 2 {
correct = true
} else {
correct = false
}
}
@IBAction func answerD(sender: UIButton) {
if optionAnswer == 3 {
correct = true
} else {
correct = false
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

回答:

回答№1は1

ロングショット-しかし、その行の関数を以前に消去しましたか?その場合は、最初の行をコメントアウトしてから、「//」を消去します。 Xcodeは時々混乱します。

補足:スイッチを使用した方がうまくいく場合があります。 また、これを構造体内に配置することを検討してください。これにより、質問に作用するメソッドrandomAnswer()を定義し、ビュー内のメソッドを参照することができます。異なるオプションは4つしかないため、列挙型として配置することもできます。 :)