缺少支架等(Swift)

时间:2016-03-05 18:10:17

标签: ios swift function curly-brackets

嗨,大家好我很难弄清楚什么是错的。在下面的函数中,当类别为Subtraction且级别为Hard时,它跳过该函数并进入除法部分。我认为我的所有措辞都是正确的,但我认为我的括号出了问题。花了一个多小时试图找到它。还是不行。顺便说一句,所有其他类别和级别都可以工作,即使在减法中也是如此。唯一错误的是减法,我找不到括号的错误。

代码:

func generate(){


    if category == "Multiplication"{
        if level == "Easy"{
    part1 = Int(arc4random_uniform(UInt32(4)))
    part2 = Int(arc4random_uniform(UInt32(4)))
    questionsLbl.text = "\(part1) x \(part2)?"
    answer = part1 * part2
    createExtraAnswers()
        }
        if level == "Medium"{
            part1 = Int(arc4random_uniform(UInt32(10)))
            part2 = Int(arc4random_uniform(UInt32(10)))
            questionsLbl.text = "\(part1) x \(part2)?"
            answer = part1 * part2
            createExtraAnswers()
        }
        if level == "Hard"{
            part1 = Int(arc4random_uniform(UInt32(14)))
            part2 = Int(arc4random_uniform(UInt32(14)))
            questionsLbl.text = "\(part1) x \(part2)?"
            answer = part1 * part2
            createExtraAnswers()
        }
    }else{
        if category == "Addition"{
            if level == "Easy"{
            part1 = Int(arc4random_uniform(UInt32(6)))
            part2 = Int(arc4random_uniform(UInt32(6)))
            questionsLbl.text = "\(part1) + \(part2)?"
            answer = part1 + part2
            createExtraAnswers()
            }
            if level == "Medium"{
                part1 = Int(arc4random_uniform(UInt32(20)))
                part2 = Int(arc4random_uniform(UInt32(20)))
                questionsLbl.text = "\(part1) + \(part2)?"
                answer = part1 + part2
                createExtraAnswers()
            }
            if level == "Hard"{
                part1 = Int(arc4random_uniform(UInt32(100)))
                part2 = Int(arc4random_uniform(UInt32(100)))
                questionsLbl.text = "\(part1) + \(part2)?"
                answer = part1 + part2
                createExtraAnswers()
            }
        }else{
            if category == "Subtraction"{
                if level == "Easy"{
                part1 = Int(arc4random_uniform(UInt32(10)))
                part2 = Int(arc4random_uniform(UInt32(10)))
                questionsLbl.text = "\(part1) - \(part2)?"
                answer = part1 - part2
                    if answer <= -1{
                        generate()
                    }
                createExtraAnswers()
                }
                if level == "Medium"{
                    part1 = Int(arc4random_uniform(UInt32(25)))
                    part2 = Int(arc4random_uniform(UInt32(25)))
                    questionsLbl.text = "\(part1) - \(part2)?"
                    answer = part1 - part2
                    if answer <= -1{
                        generate()
                    }
                    createExtraAnswers()
                }
                if level == "Hard"{
                    part1 = Int(arc4random_uniform(UInt32(100)))
                    part2 = Int(arc4random_uniform(UInt32(100)))
                    questionsLbl.text = "\(part1) - \(part2)?"
                    answer = part1 - part2

                    createExtraAnswers()

                }

            }else{
                if category == "Division"{
                    if level == "Easy"{
                    part1 = Int(arc4random_uniform(UInt32(37)))
                    part2 = Int(arc4random_uniform(UInt32(37)))
                    questionsLbl.text = "\(part1) / \(part2)?"
                    answerDub = Double(Double(part1) / Double(part2))
                  if (answerDub % 1 == 0) {
                        print("whole number confirmed")
                    print(answerDub)
                        answer = Int(answerDub)
                    print(answer)
                        createExtraAnswers()
                    } else {
                        generate()
                    }
                }
              }
                if level == "Medium"{
                    part1 = Int(arc4random_uniform(UInt32(80)))
                    part2 = Int(arc4random_uniform(UInt32(80)))
                    questionsLbl.text = "\(part1) / \(part2)?"
                    answerDub = Double(Double(part1) / Double(part2))
                    if (answerDub % 1 == 0) {
                        print("whole number confirmed")
                        print(answerDub)
                        answer = Int(answerDub)
                        print(answer)
                        createExtraAnswers()
                    } else {
                        generate()
                    }
                }
            }
            if level == "Hard"{
                part1 = Int(arc4random_uniform(UInt32(140)))
                part2 = Int(arc4random_uniform(UInt32(140)))
                questionsLbl.text = "\(part1) / \(part2)?"
                answerDub = Double(Double(part1) / Double(part2))
                if (answerDub % 1 == 0) {
                    print("whole number confirmed")
                    print(answerDub)
                    answer = Int(answerDub)
                    print(answer)
                    createExtraAnswers()
                } else {
                    generate()
                }
            }


        }
    }
}

3 个答案:

答案 0 :(得分:2)

问题出在您的if category = "Division"代码中:

if category == "Division"{
    if level == "Easy"{
        part1 = Int(arc4random_uniform(UInt32(37)))
        part2 = Int(arc4random_uniform(UInt32(37)))
        questionsLbl.text = "\(part1) / \(part2)?"
        answerDub = Double(Double(part1) / Double(part2))
        if (answerDub % 1 == 0) {
            print("whole number confirmed")
            print(answerDub)
            answer = Int(answerDub)
            print(answer)
            createExtraAnswers()
        } else {
            generate()
        }
    }
} // the if category == "Division" statement is ending here.

删除该括号(我在旁边放置评论的那个)并将其移到if level == "Hard"语句下方。

作为旁注,您的代码格式很差。如果您排列了所有块,则更容易找到错误。

答案 1 :(得分:1)

嵌套if-else语句很糟糕。为什么不使用这样的switch语句:

switch (category, level) {
case ("Multiplication", "Easy"):
    part1 = Int(arc4random_uniform(UInt32(4)))
    part2 = Int(arc4random_uniform(UInt32(4)))
    questionsLbl.text = "\(part1) x \(part2)?"
    answer = part1 * part2
    createExtraAnswers()
case ("Multiplication", "Medium"):
    part1 = Int(arc4random_uniform(UInt32(10)))
    part2 = Int(arc4random_uniform(UInt32(10)))
    questionsLbl.text = "\(part1) x \(part2)?"
    answer = part1 * part2
    createExtraAnswers()
// ...
}

这种方式更具可读性。

答案 2 :(得分:1)

我认为if-else语句会产生丑陋的不可读代码。你的用例要求你在代码中有某种类型的“丑陋”,因为你必须硬编码,所以这里有一个小小的游乐场:

enum Difficulty{
  case Easy
  case Medium
  case Hard
}

protocol ExerciseProtocol{

  var difficulty : Difficulty {get}
  var description : String {get}
  var answer : Int {get}

}

class MathExercise : ExerciseProtocol {

  enum ExerciseType : String {
    case Multiplication = "x"
    case Addition = "+"
    case Subtraction = "-"
    case Division = "/"

  }

  var difficulty : Difficulty
  var type : ExerciseType
  var operand1 : Int
  var operand2 : Int
  var description : String{

    return "\(operand1) \(self.type.rawValue) \(operand2)"

  }

  init(type: ExerciseType, difficulty: Difficulty){

    self.difficulty = difficulty
    self.type = type

    switch difficulty{
    case .Easy:
      switch type{
      case .Addition:
        operand1 = Int(arc4random_uniform(UInt32(6)))
        operand2 = Int(arc4random_uniform(UInt32(6)))
      case .Subtraction:
        operand1 = Int(arc4random_uniform(UInt32(10)))
        operand2 = Int(arc4random_uniform(UInt32(10)))
      case .Multiplication:
        operand1 = Int(arc4random_uniform(UInt32(4)))
        operand2 = Int(arc4random_uniform(UInt32(4)))
      case .Division:
        operand1 = Int(arc4random_uniform(UInt32(37)))
        operand2 = Int(arc4random_uniform(UInt32(37)))

      }
    case .Medium:
      switch type{
      case .Addition:
        operand1 = Int(arc4random_uniform(UInt32(20)))
        operand2 = Int(arc4random_uniform(UInt32(20)))
      case .Subtraction:
        operand1 = Int(arc4random_uniform(UInt32(25)))
        operand2 = Int(arc4random_uniform(UInt32(25)))
      case .Multiplication:
        operand1 = Int(arc4random_uniform(UInt32(10)))
        operand2 = Int(arc4random_uniform(UInt32(10)))
      case .Division:
        operand1 = Int(arc4random_uniform(UInt32(80)))
        operand2 = Int(arc4random_uniform(UInt32(80)))

      }
    case .Hard:
      switch type{
      case .Addition:
        operand1 = Int(arc4random_uniform(UInt32(100)))
        operand2 = Int(arc4random_uniform(UInt32(100)))
      case .Subtraction:
        operand1 = Int(arc4random_uniform(UInt32(100)))
        operand2 = Int(arc4random_uniform(UInt32(100)))
      case .Multiplication:
        operand1 = Int(arc4random_uniform(UInt32(14)))
        operand2 = Int(arc4random_uniform(UInt32(14)))
      case .Division:
        operand1 = Int(arc4random_uniform(UInt32(140)))
        operand2 = Int(arc4random_uniform(UInt32(140)))

      }
  }

  }

  var answer : Int{
    switch self.type{
    case .Addition:
      return operand1 + operand2
    case .Subtraction:
      return operand1 - operand2
    case .Multiplication:
      return operand1 * operand2
    case .Division:
      return operand1 / operand2
    }
  }
}

并像这样使用它:

let division = MathExercise(type: .Division, difficulty: .Hard)
division.description // prints something like "5 / 5"
division.answer // prints "1"