Swift 斯威夫特跳过了一句话;否则,如果;
这个Swift(Xcode)Swift 斯威夫特跳过了一句话;否则,如果;,swift,string,if-statement,return,Swift,String,If Statement,Return,这个Swift(Xcode)ChatBot程序正确地返回“hello there”和“cookies在哪里?”字符串。但是对于中间的两个,返回值“可能是任何东西”,应该是“向北!”。我认为这是一个断点或是多行上有字符串的问题,但这在Xcode workplace和Playerd中都会发生: struct MyQuestionAnswerer { func responseTo(question: String) -> String { let question =
ChatBot
程序正确地返回“hello there”和“cookies在哪里?”字符串。但是对于中间的两个,返回值“可能是任何东西”,应该是“向北!”。我认为这是一个断点或是多行上有字符串的问题,但这在Xcode workplace和Playerd中都会发生:
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"
}
}
}
您的问题是使用.lowercased()-比较两个字符串是否都是.lowercased() 此外,如果您使用案例陈述,我会觉得这个问题更好:见下文
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"}
}
}
}
您的问题是使用.lowercased()-比较两个字符串是否都是.lowercased() 此外,如果您使用案例陈述,我会觉得这个问题更好:见下文
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"}
}
}
}
请阅读我在代码块中添加的注释
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"
}
}
struct MyQuestionAnswer{
func responseTo(问题:String)->String{
让question=question.lowercased()/请阅读我在代码块中添加的注释
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"
}
}
struct MyQuestionAnswer{
func responseTo(问题:String)->String{
let question=question.lowercased()//谢谢!呸,我需要一些新鲜的眼睛来捕捉。谢谢!呸,我需要一些新鲜的眼睛来捕捉。谢谢你的建议!谢谢你的建议!