我創建了一個方法來計算作爲參數傳遞的字符串中的子字符串'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