Свифт пропускает «еще, если» - PullRequest
0 голосов
/ 26 сентября 2018

Эта программа Swift (Xcode) ChatBot правильно возвращает «привет там» и «где куки?»строки.Но для средних двух возвращается «может быть что угодно», что должно быть «На север!».Я думал, что это точка останова или проблема со строкой в ​​несколько строк, но это происходит как на рабочем месте Xcode, так и на детской площадке:

struct MyQuestionAnswerer {
    func responseTo(question: String) -> String {

        let question = question.lowercased()

        let defaultNumber = question.count % 3

        if question == "hello there" {
            return "Why hello there"
        } else if question == "where should I go on holiday?" {
            return "To the North!"
        } else if question == "where can I find the north pole?" {
            return "To the North!"
        } else if question == "where are the cookies?" {
            return "In the cookie jar!"
        } else if defaultNumber == 0 {
            return "That really depends"
        } else if defaultNumber == 1 {
            return "Ask me again tomorrow"
        } else {
            return "Could be anything"
        }
    }
}

Ответы [ 2 ]

0 голосов
/ 26 сентября 2018

Пожалуйста, прочитайте комментарии, которые я добавил в блок кода

struct MyQuestionAnswerer {
func responseTo(question: String) -> String {

    let question = question.lowercased()  // <- Converts all the strings in your question into lower case

    let defaultNumber = question.count % 3

    if question == "hello there" {
        return "Why hello there"
    } else if question == "where should I go on holiday?" {  // <- I is upper case
        return "To the North!"
    } else if question == "where can I find the north pole?" {  // <- I is upper case
        return "To the North!"
    } else if question == "where are the cookies?" {
        return "In the cookie jar!"
    } else if defaultNumber == 0 {
        return "That really depends"
    } else if defaultNumber == 1 {
        return "Ask me again tomorrow"
    } else {
        return "Could be anything"
    }
}

}

Так что, по сути, вы сравниваете "где следует i ... "to" где следует I ... ", поскольку это сравнение ложно и не соответствует ни одному другому блоку, оно проваливаетсядо последнего блока еще.

0 голосов
/ 26 сентября 2018

Ваша проблема в использовании .lowercased () - сравните с обеими строками .lowercased Также эта проблема выглядит лучше для меня, если вы используете оператор case: см. Ниже

struct MyQuestionAnswerer {
    func responseTo(question: String) -> String {

        let localQuestion = question.lowercased()
        question == String("where can I find the north pole?").lowercased()
        let defaultNumber = question.count % 3

        switch localQuestion {
        case String("hello there").lowercased() : return "Why hello there"
        case String("where should I go on holiday?").lowercased() : return "To the North!"
        case String("where can I find the north pole?").lowercased() : return "To the North!"
        case String("where are the cookies?").lowercased() : return "In the cookie jar!"
        default: if (defaultNumber == 0) {return "That really depends" } else {return "Could be anything"}
        }
    }
}
...