我创建了一个方法来计算作为参数传递的字符串中的子字符串'e'
。如果没有在字符串中的子'e'
,它应该返回"There is no \"e\"."
我试图做到这一点:调用方法不返回字符串
'e'
多少次是在一个字符串。- 如果给定字符串不包含任何
"e"
,则返回"There is no "e"."
- 如果给定字符串为空,则返回空字符串。
- 如果给定的字符串是
nil
,则返回nil
。
这是我的代码:
def find_e(s)
if !s.include?("e")
"There is no \"e\"."
elsif s.empty?
""
else s.nil?
nil
end
s.count("e").to_s
end
find_e("Bnjamin")
它跳过if
声明,它仍然采用的方法count
。为什么是这样?
您已翻转其他和elsif。 – Casey
抱歉错误的代码,我改变了。 – Benjamints
这仍然是错误的。 – Casey